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 ...
随机推荐
- ★RFC标准库_目录链接
RFC(Request For Comments)是一个国际标准化的数据库,记录了从计算机到互联网的海量标准协议.它是一个免费公开的IT标准文件分享平台,其内容也在不断增长,与时俱进.它与ISO等组织 ...
- 201521123107 《Java程序设计》第2周学习总结
第2周作业-Java基本语法与类库 1.本周学习总结 要点主要有: (1)String类 String类是本周的一个重点,String类的对象是不可变的,即String对象后就在内存中开辟了一个字符串 ...
- 201521123051《Java程序设计》第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123035《Java程序设计》第四周学习总结
本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 本周还讲了注释与类设计.老师用例子向我们展示实际生活中一个类里面包含了哪些属性,并由此联想到如果自 ...
- 201521123064 《Java程序设计》第9周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次作业题集异常 Q1:常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前 ...
- 201521123029《Java程序设计》第十三周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1.网络基础 1.1 比较ping www.baidu.com与ping cec.jmu. ...
- linux crontab详解
服务的启动和停止 cron服务是linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond ...
- 最近使用 .NET Core 遇到的一些坑
最近.NET Core升级到2.0后开始慢慢捣鼓的多了起来,但遇到了不少坑,所以特来记录下. 第一个坑 条件编译符 我们在编写一些方法的时候通常会为Debug模式增加一些输出日志等以便我们检查,也会 ...
- C++中const几中用法
转载自:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html 1. const修饰普通变量和指针 const修饰变量,一般 ...
- 一款简洁而强大的前端框架JQUery—动画效果及剪刀石头布小游戏
jQuery是什么? jQuery是一个快速.简洁的JavaScript框架,它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画 ...