JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类

1.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6     <filter>                                <!--定义核心Filter FilterDispatcher -->
 7         <filter-name>struts2</filter-name>    <!-- 定义核心Filter的名称 -->
 8         <filter-class>                        <!--定义核心Filter的实现类 -->
 9             org.apache.struts2.dispatcher.FilterDispatcher
10         </filter-class>
11     </filter>
12     <filter-mapping>
13         <filter-name>struts2</filter-name><!--核心Filter的名称 -->
14         <url-pattern>/*</url-pattern><!--使用该核心Filter过滤所有的Web请求 -->
15     </filter-mapping>
16 </web-app>

2.

 1 <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
 2 <!DOCTYPE struts PUBLIC
 3  "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 4  "http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 指定Struts 2配置文件的DTD信息 -->
 5 <struts><!-- 根节点 -->
 6     <constant name="struts.i18n.encoding" value="gb2312"></constant>
 7     <package name="struts2" extends="jfreechart-default">
 8          <action name="addVote" class="com.sanqing.action.AddVoteAction">
 9              <result name="success">/admin/addVote.jsp</result>
10          </action>
11          <action name="showVote" class="com.sanqing.action.ShowVoteAction">
12              <result name="success">/admin/showVote.jsp</result>
13          </action>
14          <action name="deleteVote" class="com.sanqing.action.DeleteVoteAction">
15              <result name="success" type="chain">showVote</result>
16          </action>
17          <action name="showVoteByChannel" class="com.sanqing.action.ShowVoteByChannelAction">
18              <result name="success">index.jsp</result>
19              <result name="input">index.jsp</result>
20          </action>
21          <action name="voteResult" class="com.sanqing.action.VoteResultAction">
22              <result name="success" type="chart">
23                  <param name="width">400</param>
24                  <param name="height">300</param>
25              </result>
26          </action>
27          <action name="doVote" class="com.sanqing.action.DoVoteAction">
28              <result name="success" type="chain">voteResult</result>
29              <result name="input" type="chain">showVoteByChannel</result>
30          </action>
31      </package>    
32 </struts>

3.jfreechart-default.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!--
 3 /*
 4  * $Id: struts-plugin.xml 651946 2008-04-27 13:41:38Z apetrelli $
 5  *
 6  * Licensed to the Apache Software Foundation (ASF) under one
 7  * or more contributor license agreements.  See the NOTICE file
 8  * distributed with this work for additional information
 9  * regarding copyright ownership.  The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License.  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied.  See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  */
23 -->
24 <!DOCTYPE struts PUBLIC
25     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
26     "http://struts.apache.org/dtds/struts-2.0.dtd">
27     
28 <struts>
29     <package name="jfreechart-default" extends="struts-default">
30     
31         <result-types>
32             <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
33                 <param name="height">150</param>
34                 <param name="width">200</param>
35             </result-type>
36         </result-types>
37     </package>
38 
39 </struts>

4.

 1 package com.sanqing.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class DBConnection {
10     private static final String DBDRIVER = "com.mysql.jdbc.Driver" ;            //驱动类类名
11     private static final String DBURL = "jdbc:mysql://localhost:3306/db_votemanage";//连接URL
12     private static final String DBUSER = "root" ;                                //数据库用户名
13     private static final String DBPASSWORD = "1234";                            //数据库密码
14     public static Connection getConnection(){
15         Connection conn = null;                                                    //声明一个连接对象
16         try {
17             Class.forName(DBDRIVER);                                            //注册驱动
18             conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);        //获得连接对象
19         } catch (ClassNotFoundException e) {                                    //捕获驱动类无法找到异常
20             e.printStackTrace();                                        
21         } catch (SQLException e) {                                                //捕获SQL异常
22             e.printStackTrace();
23         }
24         return conn;
25     }
26     public static void close(Connection conn) {//关闭连接对象
27         if(conn != null) {                //如果conn连接对象不为空
28             try {
29                 conn.close();            //关闭conn连接对象对象
30             } catch (SQLException e) {
31                 e.printStackTrace();
32             }
33         }
34     }
35     public static void close(PreparedStatement pstmt) {//关闭预处理对象
36         if(pstmt != null) {                //如果pstmt预处理对象不为空
37             try {
38                 pstmt.close();            //关闭pstmt预处理对象
39             } catch (SQLException e) {
40                 e.printStackTrace();
41             }
42         }
43     }
44     public static void close(ResultSet rs) {//关闭结果集对象
45         if(rs != null) {                //如果rs结果集对象不为null
46             try {
47                 rs.close();                //关闭rs结果集对象
48             } catch (SQLException e) {
49                 e.printStackTrace();
50             }
51         }
52     }
53 }

5.

 1 package com.sanqing.util;
 2 public class Page {
 3     private int everyPage;            //每页显示记录数
 4     private int totalCount;            //总记录数
 5     private int totalPage;            //总页数
 6     private int currentPage;        //当前页
 7     private int beginIndex;            //查询起始点
 8     private boolean hasPrePage;        //是否有上一页
 9     private boolean hasNextPage;    //是否有下一页
10     public Page(int everyPage, int totalCount, int totalPage, 
11             int currentPage,int beginIndex, boolean hasPrePage,
12             boolean hasNextPage) {    //自定义构造方法
13         this.everyPage = everyPage;
14         this.totalCount = totalCount;
15         this.totalPage = totalPage;
16         this.currentPage = currentPage;
17         this.beginIndex = beginIndex;
18         this.hasPrePage = hasPrePage;
19         this.hasNextPage = hasNextPage;
20     }
21     public Page(){}                    //默认构造函数
22     public int getEveryPage() {        //获得每页显示记录数
23         return everyPage;
24     }
25     public void setEveryPage(int everyPage) {//设置每页显示记录数
26         this.everyPage = everyPage;
27     }
28     public int getTotalCount() {//获得总记录数
29         return totalCount;
30     }
31     public void setTotalCount(int totalCount) {//设置总记录数
32         this.totalCount = totalCount;
33     }
34     public int getTotalPage() {//获得总页数
35         return totalPage;
36     }
37     public void setTotalPage(int totalPage) {//设置总页数
38         this.totalPage = totalPage;
39     }
40     public int getCurrentPage() {//获得当前页
41         return currentPage;
42     }
43     public void setCurrentPage(int currentPage) {//设置当前页
44         this.currentPage = currentPage;
45     }
46     public int getBeginIndex() {//获得查询起始点
47         return beginIndex;
48     }
49     public void setBeginIndex(int beginIndex) {//设置查询起始点
50         this.beginIndex = beginIndex;
51     }
52     public boolean isHasPrePage() {//获得是否有上一页
53         return hasPrePage;
54     }
55     public void setHasPrePage(boolean hasPrePage) {//设置是否有上一页
56         this.hasPrePage = hasPrePage;
57     }
58     public boolean isHasNextPage() {//获得是否有下一页
59         return hasNextPage;
60     }
61     public void setHasNextPage(boolean hasNextPage) {//设置是否有下一页
62         this.hasNextPage = hasNextPage;
63     }
64 }

6.

 1 package com.sanqing.util;
 2 /*
 3  * 分页信息辅助类
 4  */
 5 public class PageUtil {
 6     public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
 7         everyPage = getEveryPage(everyPage);
 8         currentPage = getCurrentPage(currentPage);
 9         int totalPage = getTotalPage(everyPage, totalCount);
10         int beginIndex = getBeginIndex(everyPage, currentPage);
11         boolean hasPrePage = getHasPrePage(currentPage);
12         boolean hasNextPage = getHasNextPage(totalPage, currentPage);
13         return new Page(everyPage, totalCount, totalPage, currentPage,
14                 beginIndex, hasPrePage,  hasNextPage);
15     }
16     public static int getEveryPage(int everyPage) {        //获得每页显示记录数
17         return everyPage == 0 ? 10 : everyPage;
18     }
19     public static int getCurrentPage(int currentPage) {    //获得当前页
20         return currentPage == 0 ? 1 : currentPage;
21     }
22     public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
23         int totalPage = 0;
24         if(totalCount != 0 &&totalCount % everyPage == 0) {
25             totalPage = totalCount / everyPage;
26         } else {
27             totalPage = totalCount / everyPage + 1;
28         }
29         return totalPage;
30     }
31     public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
32         return (currentPage - 1) * everyPage;
33     }
34     public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
35         return currentPage == 1 ? false : true;
36     }
37     public static boolean getHasNextPage(int totalPage, int currentPage) {    //获得是否有上一页
38         return currentPage == totalPage || totalPage == 0 ? false : true;
39     }
40 }

 

原文地址:https://www.cnblogs.com/shamgod/p/5319609.html