过滤器的实现
controller
@RequestMapping("/login.action")
public ModelAndView login(
@RequestParam(value="loginname",required=false)String loginname,
@RequestParam(value="password",required=false)String password,
HttpSession httpSession,
ModelAndView mv){
if(!(loginname==null||password==null)){
System.out.println(loginname+"---");
User user = userService.getByloginnameAndPassWord(loginname, password);
if(user!=null){
httpSession.setAttribute("user", user);
mv.setViewName("userList");
}else{
return new ModelAndView("redirect:/login.jsp");
}
}else{
return new ModelAndView("redirect:/login.jsp");
}
return mv;
}
filter
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
PrintWriter writer = response.getWriter();
Object user = session.getAttribute("user");
String uri = request.getRequestURI();
System.out.println(uri);
System.out.println(user);
if(!((uri.contains("login.jsp"))||(uri.contains("login.action")))){
if(user!=null){
chain.doFilter(request, response);
}else{
writer.write("<sapn color='red'>您还未登陆,三秒钟后跳转至登录页面</sapn>");
// ((HttpServletResponse) response).sendRedirect(path+"/login.jsp");
response.setHeader("refresh","3;/hrm/login.jsp");
}
}else{
chain.doFilter(request, response);
}
}
web.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">
<!-- spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc核心控制器 -->
<servlet>
<servlet-name>hrm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hrm</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.lzh.hrm.util.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
乱码问题
<!-- 编码过滤器 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
mybatis增删改查七个功能
<?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.lzh.hrm.dao.UserDao">
<sql id="key">
<trim suffixOverrides=",">
<if test="loginname!=null">
loginname,
</if>
<if test="password!=null">
password,
</if>
<if test="status!=null">
status,
</if>
<if test="createdate!=null">
createdate,
</if>
<if test="username!=null">
username,
</if>
</trim>
</sql>
<sql id="value">
<trim suffixOverrides=",">
<if test="loginname!=null">
#{loginname},
</if>
<if test="password!=null">
#{password},
</if>
<if test="status!=null">
#{status},
</if>
<if test="createdate!=null">
#{createdate},
</if>
<if test="username!=null">
#{username},
</if>
</trim>
</sql>
<select id="getById" resultType="user">
select
id,
loginname,
password,
status,
createdate,
username
from
user_inf
where
id=#{id}
</select>
<select id="getByloginnameAndPassWord" resultType="user">
select
id,
loginname,
password,
status,
createdate,
username
from
user_inf
where
loginname=#{loginname}
and
password=#{password}
</select>
<insert id="save" parameterType="user">
insert into user_inf(<include refid="key"/>) values(<include refid="value"/>)
</insert>
<delete id="delete">
delete from user_inf where id=#{id}
</delete>
<update id="update" parameterType="user">
update user_inf
<set>
<if test="loginname!=null">
loginname=#{loginname},
</if>
<if test="password!=null">
password=#{password},
</if>
<if test="status!=null">
status=#{status},
</if>
<if test="createdate!=null">
createdate=#{createdate},
</if>
<if test="username!=null">
username=#{username},
</if>
</set>
where
id=#{id}
</update>
<select id="getAll" resultType="user">
select
id,
loginname,
password,
status,
createdate,
username
from user_inf
</select>
<select id="count" resultType="int">
select
count(id)
from
user_inf
<where>
<if test="pname!=null">
username like concat(concat("%",#{pname}),"%")
</if>
</where>
</select>
<select id="getByPage" resultType="user">
select
id,
loginname,
password,
status,
createdate,
username
from
user_inf
<if test="start!=null and pageSize!=null">
limit #{start},#{pageSize}
</if>
</select>
</mapper>
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 扫描service层 -->//实现接口
<context:component-scan base-package="com.lzh.hrm.service"/>
<!-- 加载数据库配置信息 -->
<context:property-placeholder location="classpath:jdbc-mysql.properties"/>
<!-- 配置数据库连接池dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:url="${jdbc.url}"
p:driverClassName="${jdbc.driverClassName}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<!-- 配置sqlSessionFactory类 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mapperLocations="classpath:mybatis/mapper/*.xml"
p:typeAliasesPackage="com.lzh.hrm.entity"
p:configLocation="classpath:mybatis/mybatis-config.xml"/>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.lzh.hrm.dao"/>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 把事务管理器包装成advice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>//service下面所有的方法
<aop:pointcut expression="execution(* com.lzh.hrm.service..*.*(..))" id="businessService"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/>
</aop:config>
</beans>
page类
package com.lzh.hrm.util;
/**
* 分页对象
* @author shaokp
*
*/
public class Page {
/**
* 分页总数据条数
*/
private int recordCount;
/**
* 当前页
*/
private int pageIndex;
/**
* 每页显示多少条数据
*/
private int pageSize=5;
/**
* 一共多少页
*/
private int totalSize;
public int getRecordCount() {
return this.recordCount<0?0:this.recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public int getPageIndex() {
this.pageIndex=this.pageIndex<=0?1:this.pageIndex;
this.pageIndex=
this.pageIndex>this.getTotalSize()
?
this.getTotalSize()
:
this.pageIndex;
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
this.pageSize=this.pageSize<5?5:this.pageSize;
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
this.totalSize=
this.totalSize<0
?
0
:
(this.recordCount-1)/this.getPageSize()+1;
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getStart(){
return (this.pageIndex-1)*this.pageSize;
}
}
dao层测试代码
package com.lzh.hrm.dao;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import com.lzh.hrm.entity.Dept;
@ContextConfiguration("classpath:spring/applicationContext.xml")
@RunWith(SpringJUnit4Cla***unner.class)
public class DeptDaoTest {
@Autowired
private DeptDao deptDao;
@Test
public void save(){
Dept dept=new Dept("体验师","程序体验师");
deptDao.save(dept);
}
@Test
public void getById(){
Dept Dept = deptDao.getById(1);
System.out.println(Dept);
}
@Test
public void delete(){
deptDao.delete(8);
}
@Test
public void update(){
Dept Dept=new Dept("liuzhonghua","123456");
Dept.setId(9);
deptDao.update(Dept);
}
@Test
public void getAll(){
List<Dept> list=deptDao.getAll();
for(Dept u:list){
System.out.println(u);
}
}
@Test
public void count(){
Map<String, Object> map=new HashMap<String, Object>();
map.put("pname", "");
int count =deptDao.count(map);
System.out.println(count);
}
@Test
public void getByPage(){
Map<String, Object> map=new HashMap<String, Object>();
map.put("start", 0);
map.put("pageSize", 3);
List<Dept> depts = deptDao.getByPage(map);
for(Dept u:depts){
System.out.println(u);
}
}
}