一、SSM框架整合
spring+springMVC+Mybatis
spring整合mybatis 已经做过,
spring整合springMVC 可以无缝整合,本身就是一个体系,
springMVC是一个web框架,怎么把spring容器在web项目中创建,应该是在项目启动的时候创建容器,项目结束的时候关闭容器。
监听器是servlet中的一个组件,监听域对象的创建、销毁和绑定对象。
spring本身提供了一个监听context的监听器,在contxt启动的时候创建spring容器。
我们只需要在web.xml中配置监听器就可以了。
1、创建项目,导入依赖
<properties>
<spring-version>5.1.5.RELEASE</spring-version>
<jackson-versin>2.9.8</jackson-versin>
</properties>
<dependencies>
<!--spring上下文管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!--springAOP依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<!--spring-jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--织入依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- springWeb依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--springMVC转化json依赖包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-versin}</version>
</dependency>
<!--mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mybati整合spring依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
2、创建各种配置文件
1、springmvc的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
<!--扫描controller注释-->
<context:component-scan base-package="com.csdn.controller"/>
<!--配置自动加载驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--扫描springIOC的注释-->
<context:component-scan base-package="com.csdn">
<!--忽略controller注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置jdbc数据源-->
<bean id="dataSourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:127.0.0.1:3306"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
spring整合mybatis配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--sqlsessionfactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--设置别名-->
<property name="typeAliasesPackage" value="com.csdn.pojo"/>
</bean>
<!--配置扫描mapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.csdn.mapper"/>
</bean>
</beans>
在spring配置文件中添加引入:
springs事务管理配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--配置事物管理平台-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSourse"/>
</bean>
<!--配置事物的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--根据方法 配置具体的属性-->
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="insert*"/>
<tx:method name="del*"/>
<tx:method name="drop*"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="edit*"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--使用aop将事物配置到项目中-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.csdn.service..*Impl.*(..))"/>
</aop:config>
</beans>
将事务管理引入到spring核心配置:
修改web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--监听器读取文件的设置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring容器自动的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决post乱码的过滤器-->
<filter>
<filter-name>EncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
测试
二、SSM框架整合案例完成
1、创建数据库
CREATE TABLE
user
(
id
int(11) NOT NULL AUTO_INCREMENT,
nick_name
varchar(255) DEFAULT NULL,
sex
int(1) DEFAULT NULL,
age
int(3) DEFAULT NULL,
birthday
datetime DEFAULT NULL,
user_name
varchar(255) DEFAULT NULL,
password
varchar(255) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加一条数据:
2、逆向工程生成
3、创建页面
导入web资源
因为资源是静态的,在springmvc核心配置文件中添加静态资源放行:
创建登陆页面:
${pageContext.request.contextPath} jsp中一个方法 获取当前项目的路径,一般用作配置绝对路径使用
替换图片修改为绝对路径:
${pageContext.request.contextPath}/images
修改登陆界面提交的name:因为逆向工程创建的pojo是userName
4、创建登陆的方法
创建controller层
package com.csdn.controller;
import com.csdn.pojo .User;
import com.csdn.service.UserService;
import com.csdn.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/login")
public String login(User user, HttpServletRequest request){
//调用业务层实现登录的方法
boolean bl = userService.findUserByUserNameAndPwd(user,request);
//判断登陆结果实现页面跳转
if(bl){
//去首页
return "main";
}else {
//去登录面
return "forward:/login.jsp";
}
}
}
UserService:
package com.csdn.service;
import com.csdn.pojo.User;
import javax.servlet.http.HttpServletRequest;
public interface UserService {
boolean findUserByUserNameAndPwd(User user, HttpServletRequest request);
}
UserServiceImpl
package com.csdn.service.impl;
import com.csdn.mapper.UserMapper;
import com.csdn.pojo.User;
import com.csdn.pojo.UserExample;
import com.csdn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
//用户登陆的方法
@Override
public boolean findUserByUserNameAndPwd(User user, HttpServletRequest request) {
try {
//登陆的方法 创建实例类
UserExample ue = new UserExample();
//创建条件类
UserExample.Criteria criteria = ue.createCriteria();
//添加条件
criteria.andUserNameEqualTo(user.getUserName());
//查询
List<User> list = userMapper.selectByExample(ue);
//判断是否有用户名
if(!list.isEmpty()){
//获取对应的用户数据
User loginUser = list.get(0);
//对比登陆密码是否正确
if(loginUser.getPassword().equals(user.getPassword())){
//将用户数据存放到session中
request.getSession().setAttribute("login",loginUser);
//返回登录结果
return true;
}else {
//密码错误
request.setAttribute("message","登录密码错误,请重新登录");
//回显用户名
request.setAttribute("userName",user.getUserName());
}
//没有用户名 提示
request.setAttribute("message","用户名不存在,请练习管理员!!!");
}
}catch (Exception e){
//登陆失败
request.setAttribute("message","出现异常,登陆失败");
}
return false;
}
//用户登录的方法
}
5、修改登陆页面:
修改首页,在web.xml中添加
测试:
登陆失败
继续做以修改:
在userserviceimpl中修改:
测试:
创建成功之后的首页
<%--
Created by IntelliJ IDEA.
User: 呼正
Date: 2021/1/7
Time: 下午 11:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>管理平台</title>
</head>
<frameset rows="89,*,24" cols="*" framespacing="0" frameborder="no" border="0">
<frame src="${pageContext.request.contextPath}/user/forword/top" name="topFrame" scrolling="No" noresize="noresize" id="topFrame" />
<frameset cols="200,*">
<frame src="${pageContext.request.contextPath}/user/forword/left" name="leftFrame" id="leftFrame" />
<frame src="${pageContext.request.contextPath}/user/forword/center" name="mainFrame" id="mainFrame" />
</frameset>
<frame src="${pageContext.request.contextPath}/user/forword/down" name="bottomFrame" scrolling="No" noresize="noresize" id="bottomFrame" />
</frameset>
<noframes><body>
</body>
</noframes></html>
测试成功:
为什么读取不到?因为页面在WEB-INF目录下 该目录下的资源是受保护的 不能直接访问的。
controller层跳转是可以访问WEB-INF页面的:
在controller层添加一个页面跳转的方法:
修改main页面:
查看结果
修改页面所有对应相对路径
修改top页面展示登陆名和时间:
7、完成用户展示功能
创建list.jsp
添加分页插件:
修改spring整合mybatis配置:
创建后台分页查询用户的方法
控制层:
业务层:
实现类:
修改left页面:
修改list页面,分页展示数据
导入标准标签库
遍历user数据展示
最终源码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {font-size: 12px}
.STYLE4 {
font-size: 12px;
color: #1F4A65;
font-weight: bold;
}
a:link {
font-size: 12px;
color: #06482a;
text-decoration: none;
}
a:visited {
font-size: 12px;
color: #06482a;
text-decoration: none;
}
a:hover {
font-size: 12px;
color: #FF0000;
text-decoration: underline;
}
a:active {
font-size: 12px;
color: #FF0000;
text-decoration: none;
}
.STYLE7 {font-size: 12px}
-->
</style>
<script>
var highlightcolor='#eafcd5';
//此处clickcolor只能用win系统颜色代码才能成功,如果用#xxxxxx的代码就不行,还没搞清楚为什么:(
var clickcolor='#51b2f6';
function changeto(){
source=event.srcElement;
if (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor!=highlightcolor&&source.id!="nc"&&cs[1].style.backgroundColor!=clickcolor)
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=highlightcolor;
}
}
function changeback(){
if (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="nc")
return
if (event.toElement!=source&&cs[1].style.backgroundColor!=clickcolor)
//source.style.backgroundColor=originalcolor
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
function clickto(){
source=event.srcElement;
if (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor!=clickcolor&&source.id!="nc")
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=clickcolor;
}
else
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
</script>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15" height="30"><img src="${pageContext.request.contextPath}/images/tab_03.gif" width="15" height="30" /></td>
<td width="1101" background="${pageContext.request.contextPath}/images/tab_05.gif"><img src="${pageContext.request.contextPath}/images/311.gif" width="16" height="16" /> <span class="STYLE4">人员列表</span></td>
<td width="281" background="${pageContext.request.contextPath}/images/tab_05.gif"><table border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td width="60"><table width="87%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="STYLE1"><div align="center">
<input type="checkbox" name="checkbox62" value="checkbox" />
</div></td>
<td class="STYLE1"><div align="center">全选</div></td>
</tr>
</table></td>
<td width="60"><table width="90%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="STYLE1"><div align="center"><img src="${pageContext.request.contextPath}/images/001.gif" width="14" height="14" /></div></td>
<td class="STYLE1"><div align="center"><a href="add.html">新增</a></div></td>
</tr>
</table></td>
<td width="60"><table width="90%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="STYLE1"><div align="center"><img src="${pageContext.request.contextPath}/images/114.gif" width="14" height="14" /></div></td>
<td class="STYLE1"><div align="center">修改</div></td>
</tr>
</table></td>
<td width="52"><table width="88%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="STYLE1"><div align="center"><img src="${pageContext.request.contextPath}/images/083.gif" width="14" height="14" /></div></td>
<td class="STYLE1"><div align="center">删除</div></td>
</tr>
</table></td>
</tr>
</table></td>
<td width="14"><img src="${pageContext.request.contextPath}/images/tab_07.gif" width="14" height="30" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="9" background="${pageContext.request.contextPath}/images/tab_12.gif"> </td>
<td bgcolor="#f3ffe3"><table width="99%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#c0de98" onmouseover="changeto()" onmouseout="changeback()">
<tr>
<td width="6%" height="26" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2 STYLE1">选择</div></td>
<td width="8%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2 STYLE1">序号</div></td>
<td width="24%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2 STYLE1">姓名</div></td>
<td width="10%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2 STYLE1">性别</div></td>
<td width="14%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2 STYLE1">年龄</div></td>
<%--<td width="24%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2">出生日期</div></td>--%>
<td width="7%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2">编辑</div></td>
<td width="7%" height="18" background="${pageContext.request.contextPath}/images/tab_14.gif" class="STYLE1"><div align="center" class="STYLE2">删除</div></td>
</tr>
<!--数据-->
<c:forEach items="${pageInfo.list}" var="user" varStatus="vs" >
<tr>
<td height="18" bgcolor="#FFFFFF"><div align="center" class="STYLE1">
<input name="checkbox" type="checkbox" class="STYLE2" value="${user.id}" />
</div></td>
<td height="18" bgcolor="#FFFFFF" class="STYLE2"><div align="center" class="STYLE2 STYLE1">A00${user.id}</div></td>
<td height="18" bgcolor="#FFFFFF"><div align="center" class="STYLE2 STYLE1">${user.nickName}</div></td>
<td height="18" bgcolor="#FFFFFF"><div align="center" class="STYLE2 STYLE1">${user.sex eq 0 ? "女" : "男"}</div></td>
<td height="18" bgcolor="#FFFFFF"><div align="center" class="STYLE2 STYLE1">${user.age}</div></td>
<%--<td height="18" bgcolor="#FFFFFF"><div align="center" ><a href="#">
<fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"></fmt:formatDate>
</a></div></td>--%>
<td height="18" bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE2">
<img src="${pageContext.request.contextPath}/images/037.gif" width="9" height="9" />
</span>
<span class="STYLE1">[</span>
<a href="${pageContext.request.contextPath}/user/toEdit?id=${user.id}&pageNum=${pageInfo.pageNum}">编辑</a>
<span class="STYLE1">]</span>
</div>
</td>
<td height="18" bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE2">
<img src="${pageContext.request.contextPath}/images/010.gif" width="9" height="9" />
</span>
<span class="STYLE1">[</span>
<a href="#${user.id}">删除</a>
<span class="STYLE1">]</span>
</div>
</td>
</tr>
</c:forEach>
</table></td>
<td width="9" background="${pageContext.request.contextPath}/images/tab_16.gif"> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="29"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15" height="29"><img src="${pageContext.request.contextPath}/images/tab_20.gif" width="15" height="29" /></td>
<td background="${pageContext.request.contextPath}/images/tab_21.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="25%" height="29" nowrap="nowrap">
<span class="STYLE1">
共${pageInfo.total}条纪录,当前第${pageInfo.pageNum}/${pageInfo.pages}页,每页${pageInfo.pageSize}条纪录
</span></td>
<td width="75%" valign="top" class="STYLE1"><div align="right">
<table width="352" height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="62" height="22" valign="middle">
<div align="right">
<c:if test="${!pageInfo.hasPreviousPage}">
<
</c:if>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/user/page?pageNum=1"><</a>
</c:if>
<c:forEach begin="1" end="${pageInfo.pages}" step="1" var="item">
<c:if test="${pageInfo.pageNum eq item}">
${item}
</c:if>
<c:if test="${pageInfo.pageNum != item}">
<a href="${pageContext.request.contextPath}/user/page?pageNum=${item}">${item}</a>
</c:if>
</c:forEach>
<c:if test="${!pageInfo.hasNextPage}">
>
</c:if>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/user/page?pageNum=${pageInfo.pages}">></a>
</c:if>
</div></td>
</tr>
</table>
</div></td>
</tr>
</table></td>
<td width="14"><img src="${pageContext.request.contextPath}/images/tab_22.gif" width="14" height="29" /></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
添加分页数据
查看:
8、完成修改功能
控制层添加方法
添加业务层的方法:
找到user对象然后再实现修改
控制层添加方法
添加业务层的方法:
添加edit页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {font-size: 12px}
.STYLE4 {
font-size: 12px;
color: #1F4A65;
font-weight: bold;
}
a:link {
font-size: 12px;
color: #06482a;
text-decoration: none;
}
a:visited {
font-size: 12px;
color: #06482a;
text-decoration: none;
}
a:hover {
font-size: 12px;
color: #FF0000;
text-decoration: underline;
}
a:active {
font-size: 12px;
color: #FF0000;
text-decoration: none;
}
.STYLE7 {font-size: 12px}
-->
</style>
<script type="text/javascript" src="${pageContext.request.contextPath}/My97DatePicker/WdatePicker.js"></script>
<script>
var highlightcolor='#eafcd5';
//此处clickcolor只能用win系统颜色代码才能成功,如果用#xxxxxx的代码就不行,还没搞清楚为什么:(
var clickcolor='#51b2f6';
function changeto(){
source=event.srcElement;
if (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor!=highlightcolor&&source.id!="nc"&&cs[1].style.backgroundColor!=clickcolor)
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=highlightcolor;
}
}
function changeback(){
if (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="nc")
return
if (event.toElement!=source&&cs[1].style.backgroundColor!=clickcolor)
//source.style.backgroundColor=originalcolor
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
function clickto(){
source=event.srcElement;
if (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor!=clickcolor&&source.id!="nc")
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=clickcolor;
}
else
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
</script>
</head>
<body>
<table width="100%" border="0" bgcolor="#E7FAD0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15" height="30"><img src="${pageContext.request.contextPath}/imagestab_03.gif" width="15" height="30" /></td>
<td width="1101" background="${pageContext.request.contextPath}/imagestab_05.gif"><img src="${pageContext.request.contextPath}/images311.gif" width="16" height="16" /> <span class="STYLE4">添加人员</span></td>
<td width="281" background="${pageContext.request.contextPath}/imagestab_05.gif"><table border="0" align="right" cellpadding="0" cellspacing="0">
</table></td>
<td width="14"><img src="${pageContext.request.contextPath}/imagestab_07.gif" width="14" height="30" /></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<form action="${pageContext.request.contextPath}/user/edit" method="post">
<input type="hidden" name="id" value="${user.id}">
<input type="hidden" name="pageNum" value="${pageNum}">
<table align="center">
<tr>
<td align="right">姓名:</td>
<td><input type="text" name="nickName" value="${user.nickName}"/></td>
</tr>
<tr>
<td align="right">性别:</td>
<td>
<input type="radio" name="sex" value="1" ${user.sex eq 1 ? "checked":""}/>男
<input type="radio" name="sex" value="0" ${user.sex eq 0 ? "checked":""}/>女
</td>
</tr>
<tr>
<td align="right">登陆名:</td>
<td>
<input type="text" name="userName" value="${user.userName}">
</td>
</tr>
<tr>
<td align="right">密码:</td>
<td>
<input type="text" name="password" value="${user.password}">
</td>
</tr>
<tr>
<td align="right">年龄:</td>
<td><input type="text" name="age" value="${user.age}"/></td>
</tr>
<%-- <tr>
<td align="right">出生日期:</td>
<td>
<input type="text" name="birthday" id="birthday"
value="<fmt:formatDate value="${user.birthday}" pattern='yyyy-MM-dd' />"
onfocus="WdatePicker({el:'birthday'})" readonly="readonly"/>
</tr>--%>
<tr>
<td colspan="2" align="center"><input type="submit" value="修改"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
修改:
修改list页面,编辑的a标签:
测试
这里我的pojo因为逆向工程忘了将birthday加入,故页面没有展示,如果展示会报错500;在这里修改的时候如果有birthday日期类型格式会报错400
为什么会报错呢?
因为时间转化异常:将之前的时间转化器添加进来:
package com.csdn.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
//预留规则
String param = "";
//判断是- \
if (source.indexOf("-") > 0){
//判断长度
if(source.length() > 10){
param = "yyyy-MM-dd HH:mm:ss";
}else{
param = "yyyy-MM-dd";
}
}else if(source.indexOf("\\") > 0){
//判断长度
if(source.length() > 10){
param = "yyyy\\MM\\dd HH:mm:ss";
}else{
param = "yyyy\\MM\\dd";
}
}
//创建时间格式化类
SimpleDateFormat sdf = new SimpleDateFormat(param);
//转化时间
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
}
修改springmvc的配置文件:
重启项目解决~
9、添加拦截器
问题:如果没有登陆通过url一样使用项目功能;
添加登陆拦截器,实现对应操作:
在psringMVC核心配置文件中添加:
测试: