SSM框架整合项目 :投票系统
框架:
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
<?xml version="1.0" encoding="UTF-8"?>
<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"> <!-- 告诉web容器log4j的属性文件所在位置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> <!-- spring框架提供的字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加载springmvc配置文件 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 引入数据库信息的属性文件 -->
<context:property-placeholder location="classpath:conf/db_orcl.properties" /> <!-- 配置扫描器 -->
<context:component-scan base-package="com.hanqi.dao.impl" /> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean> <!-- 配置Mybatis核心对象 SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis中使用的别名 -->
<property name="typeAliasesPackage" value="com.hanqi.model"></property>
<!-- 注入数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- Mybatis需要的映射文件 -->
<property name="mapperLocations" value="classpath:com/hanqi/dao/impl/*Mapper.xml"></property>
</bean> <!-- mybatis所有的映射接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hanqi.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 配置扫描器 -->
<context:component-scan base-package="com.hanqi.controller" /> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp"></bean> <!-- 开启mvc注解驱动 -->
<mvc:annotation-driven>
<!-- springMVC有一个默认的json格式的转换器, 是基于Jackson.jar, 但实际开发当中, fastjson -->
<mvc:message-converters register-defaults="false">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 配置支持的媒体类型 -->
<property name="supportedMediaTypes">
<list>
<!-- 顺序不能写反, 否则会出现下载提示 -->
<value>text/html; charset=utf-8</value>
<value>application/json; charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
包结构文件:
model层---实体类:
package com.hanqi.model;
public class XiangMu {
private String ids;
private String title;
private String selectiontype;
private String isover;
public XiangMu(String ids, String title, String selectiontype, String isover) {
super();
this.ids = ids;
this.title = title;
this.selectiontype = selectiontype;
this.isover = isover;
}
public XiangMu() {
super();
// TODO Auto-generated constructor stub
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSelectiontype() {
return selectiontype;
}
public void setSelectiontype(String selectiontype) {
this.selectiontype = selectiontype;
}
public String getIsover() {
return isover;
}
public void setIsover(String isover) {
this.isover = isover;
}
@Override
public String toString() {
return "XiangMu [ids=" + ids + ", title=" + title + ", selectiontype=" + selectiontype + ", isover=" + isover
+ "]";
}
}
package com.hanqi.model;
public class XuanXiang {
private String ids;
private String options;
private Integer numbers;
private String timudaihao;
private String baifenb;
public XuanXiang() {
super();
// TODO Auto-generated constructor stub
}
public XuanXiang(String ids, String options, Integer numbers, String timudaihao) {
super();
this.ids = ids;
this.options = options;
this.numbers = numbers;
this.timudaihao = timudaihao;
}
public String getBaifenb() {
return baifenb;
}
public void setBaifenb(String baifenb) {
this.baifenb = baifenb;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getOptions() {
return options;
}
public void setOptions(String options) {
this.options = options;
}
public Integer getNumbers() {
return numbers;
}
public void setNumbers(Integer numbers) {
this.numbers = numbers;
}
public String getTimudaihao() {
return timudaihao;
}
public void setTimudaihao(String timudaihao) {
this.timudaihao = timudaihao;
}
@Override
public String toString() {
return "XuanXiang [ids=" + ids + ", options=" + options + ", numbers=" + numbers + ", xiangmuid=" + timudaihao
+ "]";
}
}
dao层---接口:
package com.hanqi.dao; import java.util.List; import com.hanqi.model.XiangMu;
import com.hanqi.model.XuanXiang; public interface TouPiaoDao { List<XiangMu> selectAllXiangMu(); List<XuanXiang> selectAllXuanXiang(); int updatexuanxiang(String xuanxiang,int i); int selectXuanXiang(String xuanxiang);
}
dao层---实现方法:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanqi.dao.TouPiaoDao"> <select id="selectAllXiangMu" resultType="XiangMu">
select t.* from DIAOYANTIMU t where t.ids=101
</select>
<select id="selectAllXuanXiang" resultType="XuanXiang" parameterType="String">
select t.* from DIAOYANXUANXIANG t where t.timudaihao=101
</select>
<select id="selectXuanXiang" resultType="Integer">
select t.numbers from DIAOYANXUANXIANG t where t.options=#{xuanxiang}
</select> <update id="updatexuanxiang">
update DIAOYANXUANXIANG t set t.numbers=#{param2} where t.options=#{param1}
</update> </mapper>
controller层---控制器:
package com.hanqi.controller; import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSONObject;
import com.hanqi.dao.TouPiaoDao;
import com.hanqi.model.XiangMu;
import com.hanqi.model.XuanXiang; @Controller
@SessionAttributes("currentUser")
@RequestMapping("/toupiao")
public class ToupiaoController { @Autowired
private TouPiaoDao tpDao; //@ResponseBody
@RequestMapping("/selectall")
public String selectAll(Model model) {
List<XiangMu> xmlist=tpDao.selectAllXiangMu();
List<XuanXiang> xxlist=tpDao.selectAllXuanXiang();
model.addAttribute("xmlist", xmlist);
model.addAttribute("xxlsit", xxlist);
return "second";
} @RequestMapping("/calcpiao")
public String calcPiao(String xuanxiang,Model model) { List<XuanXiang> xxlist1=tpDao.selectAllXuanXiang();
int i=0;
for(XuanXiang x:xxlist1){
if(xuanxiang.equals(x.getOptions())){
System.out.println("---1----");
i=tpDao.selectXuanXiang(xuanxiang);
i++;
int c=tpDao.updatexuanxiang(xuanxiang,i);
}
} List<XiangMu> xmlist=tpDao.selectAllXiangMu();
List<XuanXiang> xxlist=tpDao.selectAllXuanXiang();
double sum=0;
for(XuanXiang x:xxlist){
sum=sum+x.getNumbers();
}
NumberFormat nf= NumberFormat.getInstance(); //创建格式化类nf
nf.setMaximumFractionDigits(2); //数值2表示保留2位小数
for(XuanXiang x:xxlist){ x.setBaifenb(nf.format(
(double)x.getNumbers()/sum*100));
} model.addAttribute("xmlist", xmlist);
model.addAttribute("xxlsit", xxlist);
model.addAttribute("sum", sum); return "second";
} }
前台jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
String path=request.getContextPath();
%>
</head>
<body>
<div style="text-align: center">
<div><h1>登錄頁名</h1></div> <div style="margin-top: 100px">
<a href="<%=path %>/toupiao/selectall.do">查看所有投票項目</a><br>
<a>專業調查</a><br>
<a>技術調查</a><br> </div>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
String path = request.getContextPath();
%>
</head>
<body>
<table style="text-align: center">
<form action="<%=path %>/toupiao/calcpiao.do" method="post">
<c:forEach var="xiangmu" items="${xmlist }" >
<tr>
<td>${xiangmu.title }</td>
<td>
<ul>
<c:forEach var="xuanxiang" items="${xxlsit }">
<li><input type="radio" name="xuanxiang" value="${xuanxiang.options }"> ${xuanxiang.options }
<div style="width: 100px;height:40px"><br>
<div style="width: ${xuanxiang.baifenb}%;height:20px;background: green">
</div>
${xuanxiang.numbers} ( ${xuanxiang.baifenb}% )
</div>
</li>
</c:forEach>
</ul>
</td>
</tr> </c:forEach>
<input type="submit" value="提交">
</form>
</table>
</body>
</html>
效果:


总结:
1.前台传值乱码
从前台表单中传到controller层控制器中的变量,如果不加设置会是乱码
post方式和get方式提交的设置方法不同
这里用的是post方式,方法在spring-mvc.xml中已经设置好
gei方式需要修改xml文件
2.百分比的计算
经过尝试,没有在前台JSP页面中使用EL表达式将百分比计算出来,百度也没有结果,只能进行变量和数字的计算,没有两个变量之间的计算
只能在选项类定义一个百分比的成员变量
3.int型之间的除法结果是无穷大
需要都设置double型
4.取计算结果小数点后几位
NumberFormat nf= NumberFormat.getInstance(); //创建格式化类
nf.setMaximumFractionDigits(2); //数值2表示保留2位小数
nf.format((double)x.getNumbers()/sum*100)); //结果是字符串
5.多选和单选
这里只写了单选,多选基本上差不多,需要注意传入的是数组,修改一下具体实现的代码
6.Oracle数据库中计算+1
不知道为什么直接在sql语句中计算总是报错,只能计算之后写入
SSM框架整合项目 :投票系统的更多相关文章
- SSM框架整合项目 :租房管理系统
使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...
- SpringMVC详解及SSM框架整合项目
SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...
- IDEA使用maven搭建SSM框架整合项目(超级详细,值得一看)
目录 温馨提示 简单介绍下SSM 搭建过程 一.框架介绍 二.下载Maven 三.创建Maven项目 四.Maven工程需要引入的Jar 包 五.整合SSM框架.需要的相关配置文件配置项目 六.工程导 ...
- JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合
1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...
- 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- SSM(Spring,SpringMVC,Mybatis)框架整合项目
快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...
- SSM框架整合图书管理项目
SSM框架整合 1.建立简单的maven项目 2.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <p ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
随机推荐
- PHP初入,for循环使用
一: 找出100-999之间的所有"水仙花数".所谓水仙花数是指一个三位 数,各位数字的立方和等于该数本身.(如153次方=1的3次方+5的3次方+3的3次方)并输出这些数字 想想 ...
- JS嵌套循环的典型练习题
1.斐波那契数列 ①分数 <script type="text/javascript"> var a = 1 var b = 1 var c for ...
- Web应用程序的开发步骤
Web应用程序的开发步骤 如今已进入了web2.0高速发展的互联网时代,各种互联网的Web应用程序如雨后春笋般出现.那么作为一名Web开发人员,怎样去开发一款优秀的Web应用程序呢?这个问题没有一个简 ...
- Python并发编程协程(Coroutine)之Gevent
Gevent官网文档地址:http://www.gevent.org/contents.html 基本概念 我们通常所说的协程Coroutine其实是corporate routine的缩写,直接翻译 ...
- Java单元测试之覆盖率统计eclemma
安装 有两种安装方法 下载安装(推荐) 地址: http://sourceforge.net/projects/eclemma/ 将解压后的features和plugins目录下的文件分别拷贝到Ecl ...
- week2-结对编程【网页实现四则运算】
题目描述: 不知道大家是否尝试过这样一种开发模式:你有一个伙伴,你们坐在一起,并肩作战,面对着同一台显示器,使用着同一键盘,同一个鼠标,你们一起思考,一起分析,一起编程?这次,就让我们来体验一下结对编 ...
- 201521123011 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 public ...
- [ JDK ] 列表转数组 toArray
<T> T[] toArray(T[] a) :该方法返回一个数组,数组元素包含了 List<T> 中的所有元素,数组中的元素顺序和 List<T> 中的元素顺序保 ...
- python基础之socket
一.osi七层 完整的计算机系统由硬件,操作系统,软件组成. 互联网的核心就是由一堆协议组成,协议就是标准,如全世界通信的标准就是英语. 如果把计算机比作人,那么互联网协议就是计算机界的英语,所有计算 ...
- 关于centOS 7的服务启动,端口查询,防火墙管理
端口的查询与开启 CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的CentOS 7 采用了 firewalld 防火墙 如要查询是否开启8 ...