框架:

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框架整合项目 :投票系统的更多相关文章

  1. SSM框架整合项目 :租房管理系统

    使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...

  2. SpringMVC详解及SSM框架整合项目

    SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...

  3. IDEA使用maven搭建SSM框架整合项目(超级详细,值得一看)

    目录 温馨提示 简单介绍下SSM 搭建过程 一.框架介绍 二.下载Maven 三.创建Maven项目 四.Maven工程需要引入的Jar 包 五.整合SSM框架.需要的相关配置文件配置项目 六.工程导 ...

  4. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  5. 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  6. 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  7. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  8. SSM框架整合图书管理项目

    SSM框架整合 1.建立简单的maven项目 2.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <p ...

  9. 基于maven的ssm框架整合

    基于maven的ssm框架整合 第一步:通过maven建立一个web项目.                第二步:pom文件导入jar包                              (1 ...

随机推荐

  1. 201521123084 《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. (1)Java中把不同类型的输入.输出抽象为流(Stream),而其中输入.输出的数据则称为数据流(Data ...

  2. Quartz2.2.x官方教程

    零.Quartz是什么?能干什么? Quartz是一个开源的任务调度框架.基于定时.定期的策略来执行任务是它的核心功能,比如x年x月的每个星期五上午8点到9点,每隔10分钟执行1次.Quartz有3个 ...

  3. 201521123063 《Java程序设计》 第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 finally 题目4-2 1.1 截图你的提交结果(出现学 ...

  4. 201521123042《Java程序设计》第13周学习总结

    本次作业参考文件 正则表达式参考资料 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.bai ...

  5. 201521123054 《Java程序设计》第9周学习总结

    1. 本周学习总结 2. 书面作业 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 经常会出现ArrayIndexO ...

  6. 201521123122 《java程序设计》第十一周学习总结

    ## 201521123122 <java程序设计>第十一周实验总结 ## 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 其实这周也没讲多少内容,所 ...

  7. 201521123026 《java程序设计》 第九周学习总结

    1. 本章学习总结 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 Q1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) PTA提交结果: 运行结果截图: 1.2 ...

  8. 201521123085《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 finally 题目4-2 1.1 截图你的提交结果(出现学 ...

  9. JavaScript中的for in循环

    在学习AJAX的时候,发现JavaScript中for in循环,这种循环对于遍历JSON是很好用的.于是写下了这篇博文 作用 for in循环本质上是forEach循环,它主要有两个作用 遍历数组 ...

  10. 关于linux下的文件权限

    在ls指令加 -l 参数能看到文件权限 就像这样: drwxrwxr-x 2 asml users 4096 Jul 24 02:45 desktop 第一个d表示这是个目录,若为"-&qu ...