框架:
Spring
SpringMVC
MyBatis
题目:
投票系统
导包:
1, spring
2, MyBatis
3, mybatis-spring
4, fastjson
5, aspectweaver----AspectJ框架
6, log4j-----打印日志信息
7, ojdbc6.jar
8, jstl.jar, standard.jar----标准标签库
9, commons-logging-1.2.jar
10,……
建立包结构
配置web.xml,spring-mvc.xml,spring-all.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
3
4 <!-- 告诉web容器log4j的属性文件所在位置 -->
5 <context-param>
6 <param-name>log4jConfigLocation</param-name>
7 <param-value>classpath:conf/log4j.properties</param-value>
8 </context-param>
9 <listener>
10 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
11 </listener>
12
13 <!-- spring框架提供的字符编码过滤器 -->
14 <filter>
15 <filter-name>characterEncodingFilter</filter-name>
16 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
17 <init-param>
18 <param-name>encoding</param-name>
19 <param-value>UTF-8</param-value>
20 </init-param>
21 <init-param>
22 <param-name>forceEncoding</param-name>
23 <param-value>true</param-value>
24 </init-param>
25 </filter>
26 <filter-mapping>
27 <filter-name>characterEncodingFilter</filter-name>
28 <url-pattern>/*</url-pattern>
29 </filter-mapping>
30
31 <!-- 加载spring配置文件 -->
32 <context-param>
33 <param-name>contextConfigLocation</param-name>
34 <param-value>classpath:conf/spring-config.xml</param-value>
35 </context-param>
36 <listener>
37 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
38 </listener>
39
40 <!-- 加载springmvc配置文件 -->
41 <servlet>
42 <servlet-name>springDispatcherServlet</servlet-name>
43 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
44 <init-param>
45 <param-name>contextConfigLocation</param-name>
46 <param-value>classpath:conf/spring-mvc.xml</param-value>
47 </init-param>
48 <load-on-startup>1</load-on-startup>
49 </servlet>
50 <servlet-mapping>
51 <servlet-name>springDispatcherServlet</servlet-name>
52 <url-pattern>*.do</url-pattern>
53 </servlet-mapping>
54 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xmlns:p="http://www.springframework.org/schema/p"
7 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
10
11 <!-- 引入数据库信息的属性文件 -->
12 <context:property-placeholder location="classpath:conf/db_orcl.properties" />
13
14 <!-- 配置扫描器 -->
15 <context:component-scan base-package="com.hanqi.dao.impl" />
16
17 <!-- 配置数据源 -->
18 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19 <property name="username" value="${jdbc.username}"></property>
20 <property name="password" value="${jdbc.password}"></property>
21 <property name="driverClassName" value="${jdbc.driverClassName}"></property>
22 <property name="url" value="${jdbc.url}"></property>
23 </bean>
24
25 <!-- 配置Mybatis核心对象 SessionFactory -->
26 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
27 <!-- mybatis中使用的别名 -->
28 <property name="typeAliasesPackage" value="com.hanqi.model"></property>
29 <!-- 注入数据源属性 -->
30 <property name="dataSource" ref="dataSource"></property>
31 <!-- Mybatis需要的映射文件 -->
32 <property name="mapperLocations" value="classpath:com/hanqi/dao/impl/*Mapper.xml"></property>
33 </bean>
34
35 <!-- mybatis所有的映射接口 -->
36 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
37 <property name="basePackage" value="com.hanqi.dao"></property>
38 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
39 </bean>
40 </beans>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
8
9 <!-- 配置扫描器 -->
10 <context:component-scan base-package="com.hanqi.controller" />
11
12 <!-- 配置视图解析器 -->
13 <bean
14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
15 p:prefix="/" p:suffix=".jsp"></bean>
16
17 <!-- 开启mvc注解驱动 -->
18 <mvc:annotation-driven>
19 <!-- springMVC有一个默认的json格式的转换器, 是基于Jackson.jar, 但实际开发当中, fastjson -->
20 <mvc:message-converters register-defaults="false">
21 <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
22 <!-- 配置支持的媒体类型 -->
23 <property name="supportedMediaTypes">
24 <list>
25 <!-- 顺序不能写反, 否则会出现下载提示 -->
26 <value>text/html; charset=utf-8</value>
27 <value>application/json; charset=utf-8</value>
28 </list>
29 </property>
30 </bean>
31 </mvc:message-converters>
32 </mvc:annotation-driven>
33 </beans>
包结构文件:
model层---实体类:
1 package com.hanqi.model;
2
3 public class XiangMu {
4
5 private String ids;
6 private String title;
7 private String selectiontype;
8 private String isover;
9
10 public XiangMu(String ids, String title, String selectiontype, String isover) {
11 super();
12 this.ids = ids;
13 this.title = title;
14 this.selectiontype = selectiontype;
15 this.isover = isover;
16 }
17
18 public XiangMu() {
19 super();
20 // TODO Auto-generated constructor stub
21 }
22
23 public String getIds() {
24 return ids;
25 }
26
27 public void setIds(String ids) {
28 this.ids = ids;
29 }
30
31 public String getTitle() {
32 return title;
33 }
34
35 public void setTitle(String title) {
36 this.title = title;
37 }
38
39 public String getSelectiontype() {
40 return selectiontype;
41 }
42
43 public void setSelectiontype(String selectiontype) {
44 this.selectiontype = selectiontype;
45 }
46
47 public String getIsover() {
48 return isover;
49 }
50
51 public void setIsover(String isover) {
52 this.isover = isover;
53 }
54
55 @Override
56 public String toString() {
57 return "XiangMu [ids=" + ids + ", title=" + title + ", selectiontype=" + selectiontype + ", isover=" + isover
58 + "]";
59 }
60
61 }
1 package com.hanqi.model;
2
3 public class XuanXiang {
4
5 private String ids;
6 private String options;
7 private Integer numbers;
8 private String timudaihao;
9 private String baifenb;
10
11 public XuanXiang() {
12 super();
13 // TODO Auto-generated constructor stub
14 }
15
16 public XuanXiang(String ids, String options, Integer numbers, String timudaihao) {
17 super();
18 this.ids = ids;
19 this.options = options;
20 this.numbers = numbers;
21 this.timudaihao = timudaihao;
22 }
23
24 public String getBaifenb() {
25 return baifenb;
26 }
27
28 public void setBaifenb(String baifenb) {
29 this.baifenb = baifenb;
30 }
31
32 public String getIds() {
33 return ids;
34 }
35
36 public void setIds(String ids) {
37 this.ids = ids;
38 }
39
40 public String getOptions() {
41 return options;
42 }
43
44 public void setOptions(String options) {
45 this.options = options;
46 }
47
48 public Integer getNumbers() {
49 return numbers;
50 }
51
52 public void setNumbers(Integer numbers) {
53 this.numbers = numbers;
54 }
55
56 public String getTimudaihao() {
57 return timudaihao;
58 }
59
60 public void setTimudaihao(String timudaihao) {
61 this.timudaihao = timudaihao;
62 }
63
64 @Override
65 public String toString() {
66 return "XuanXiang [ids=" + ids + ", options=" + options + ", numbers=" + numbers + ", xiangmuid=" + timudaihao
67 + "]";
68 }
69
70 }
dao层---接口:
1 package com.hanqi.dao;
2
3 import java.util.List;
4
5 import com.hanqi.model.XiangMu;
6 import com.hanqi.model.XuanXiang;
7
8 public interface TouPiaoDao {
9
10 List<XiangMu> selectAllXiangMu();
11
12 List<XuanXiang> selectAllXuanXiang();
13
14 int updatexuanxiang(String xuanxiang,int i);
15
16 int selectXuanXiang(String xuanxiang);
17 }
dao层---实现方法:
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.hanqi.dao.TouPiaoDao">
6
7 <select id="selectAllXiangMu" resultType="XiangMu">
8 select t.* from DIAOYANTIMU t where t.ids=101
9 </select>
10 <select id="selectAllXuanXiang" resultType="XuanXiang" parameterType="String">
11 select t.* from DIAOYANXUANXIANG t where t.timudaihao=101
12 </select>
13 <select id="selectXuanXiang" resultType="Integer">
14 select t.numbers from DIAOYANXUANXIANG t where t.options=#{xuanxiang}
15 </select>
16
17 <update id="updatexuanxiang">
18 update DIAOYANXUANXIANG t set t.numbers=#{param2} where t.options=#{param1}
19 </update>
20
21 </mapper>
controller层---控制器:
1 package com.hanqi.controller;
2
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Controller;
9 import org.springframework.ui.Model;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 import org.springframework.web.bind.annotation.SessionAttributes;
13 import org.springframework.web.servlet.ModelAndView;
14
15 import com.alibaba.fastjson.JSONObject;
16 import com.hanqi.dao.TouPiaoDao;
17 import com.hanqi.model.XiangMu;
18 import com.hanqi.model.XuanXiang;
19
20 @Controller
21 @SessionAttributes("currentUser")
22 @RequestMapping("/toupiao")
23 public class ToupiaoController {
24
25 @Autowired
26 private TouPiaoDao tpDao;
27
28 //@ResponseBody
29 @RequestMapping("/selectall")
30 public String selectAll(Model model) {
31 List<XiangMu> xmlist=tpDao.selectAllXiangMu();
32 List<XuanXiang> xxlist=tpDao.selectAllXuanXiang();
33 model.addAttribute("xmlist", xmlist);
34 model.addAttribute("xxlsit", xxlist);
35 return "second";
36 }
37
38
39 @RequestMapping("/calcpiao")
40 public String calcPiao(String xuanxiang,Model model) {
41
42 List<XuanXiang> xxlist1=tpDao.selectAllXuanXiang();
43 int i=0;
44 for(XuanXiang x:xxlist1){
45 if(xuanxiang.equals(x.getOptions())){
46 System.out.println("---1----");
47 i=tpDao.selectXuanXiang(xuanxiang);
48 i++;
49 int c=tpDao.updatexuanxiang(xuanxiang,i);
50 }
51 }
52
53 List<XiangMu> xmlist=tpDao.selectAllXiangMu();
54 List<XuanXiang> xxlist=tpDao.selectAllXuanXiang();
55 double sum=0;
56 for(XuanXiang x:xxlist){
57 sum=sum+x.getNumbers();
58 }
59 NumberFormat nf= NumberFormat.getInstance(); //创建格式化类nf
60 nf.setMaximumFractionDigits(2); //数值2表示保留2位小数
61 for(XuanXiang x:xxlist){
62
63 x.setBaifenb(nf.format(
64 (double)x.getNumbers()/sum*100));
65 }
66
67 model.addAttribute("xmlist", xmlist);
68 model.addAttribute("xxlsit", xxlist);
69 model.addAttribute("sum", sum);
70
71 return "second";
72 }
73
74 }
前台jsp页面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Insert title here</title>
8 <%
9 String path=request.getContextPath();
10 %>
11 </head>
12 <body>
13 <div style="text-align: center">
14 <div><h1>登錄頁名</h1></div>
15
16 <div style="margin-top: 100px">
17 <a href="<%=path %>/toupiao/selectall.do">查看所有投票項目</a><br>
18 <a>專業調查</a><br>
19 <a>技術調查</a><br>
20
21 </div>
22 </div>
23 </body>
24 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8 <title>Insert title here</title>
9 <%
10 String path = request.getContextPath();
11 %>
12 </head>
13 <body>
14 <table style="text-align: center">
15 <form action="<%=path %>/toupiao/calcpiao.do" method="post">
16 <c:forEach var="xiangmu" items="${xmlist }" >
17 <tr>
18 <td>${xiangmu.title }</td>
19 <td>
20 <ul>
21 <c:forEach var="xuanxiang" items="${xxlsit }">
22 <li><input type="radio" name="xuanxiang" value="${xuanxiang.options }"> ${xuanxiang.options }
23 <div style="width: 100px;height:40px"><br>
24 <div style="width: ${xuanxiang.baifenb}%;height:20px;background: green">
25 </div>
26 ${xuanxiang.numbers} ( ${xuanxiang.baifenb}% )
27 </div>
28 </li>
29 </c:forEach>
30 </ul>
31 </td>
32 </tr>
33
34 </c:forEach>
35 <input type="submit" value="提交">
36 </form>
37 </table>
38 </body>
39 </html>
效果:
总结:
1.前台传值乱码
从前台表单中传到controller层控制器中的变量,如果不加设置会是乱码
post方式和get方式提交的设置方法不同
这里用的是post方式,方法在spring-mvc.xml中已经设置好
gei方式需要修改xml文件
2.百分比的计算
经过尝试,没有在前台JSP页面中使用EL表达式将百分比计算出来,百度也没有结果,只能进行变量和数字的计算,没有两个变量之间的计算
只能在选项类定义一个百分比的成员变量
3.int型之间的除法结果是无穷大
需要都设置double型
4.取计算结果小数点后几位
1 NumberFormat nf= NumberFormat.getInstance(); //创建格式化类
2 nf.setMaximumFractionDigits(2); //数值2表示保留2位小数
3 nf.format((double)x.getNumbers()/sum*100)); //结果是字符串
5.多选和单选
这里只写了单选,多选基本上差不多,需要注意传入的是数组,修改一下具体实现的代码
6.Oracle数据库中计算+1
不知道为什么直接在sql语句中计算总是报错,只能计算之后写入