Struts2_拦截器_登录拦截
一、1.jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-chain-1.2.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
jcommon-1.0.23.jar
jfreechart-1.0.19.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.8-bin.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
struts2-jfreechart-plugin-2.3.4.1.jar
struts2-json-plugin-2.3.4.1.jar
xwork-core-2.3.15.3.jar
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置核心控制器 -->
<filter>
<filter-name>strutsFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.interceptor编写
package cn.itcast.web.interceptor;
import java.util.Map;
import cn.itcast.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Map<String, Object> map = ActionContext.getContext().getSession();
User user = (User)map.get("LoginUser");//从session中获取user对象
if(user==null){//判断该user对象是否存在
return "login";//返回到全局
}
String result = invocation.invoke();//执行已经验证登录的功能
return result;
}
}
4.action编写 (1)UserLoginAction package cn.itcast.web.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import cn.itcast.domain.User; import cn.itcast.service.UserService; import cn.itcast.service.Impl.UserServiceImpl; import cn.itcast.utils.BaseAction;
public class UserLoginAction extends BaseAction{
private User user = new User();//声明接收到的用户对象
private String msg;//声明向页面回显的信息
private UserService userService = new UserServiceImpl();
@Override
public String execute() throws Exception {
Boolean result = userService.findUser(user);//查询该用户对象是否存在
if(result){//如果用户存在
Map<String, Object> map = ActionContext.getContext().getSession();
map.put("LoginUser", user);//将用户信息存放到session中
return SUCCESS;
}
msg = "用户名或者密码错误!";//否则,向页面提示错误信息
return "fail";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
(2)
package cn.itcast.web.action;
import java.util.List;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.Impl.UserServiceImpl;
import cn.itcast.utils.BaseAction;
public class UserAction extends BaseAction{
private UserService userService = new UserServiceImpl();
private List<User> users;//声明向list页面传输的数据
@Override
public String execute() throws Exception {
users = userService.queryUsers();//查询用户列表
return SUCCESS;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
5.service编写
package cn.itcast.service.Impl;
import java.util.List;
import cn.itcast.dao.UserDao;
import cn.itcast.dao.Impl.UserDaoImpl;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
public Boolean findUser(User user) {
// TODO Auto-generated method stub
Boolean result = userDao.findUser(user);
return result;
}
public List<User> queryUsers() {
// TODO Auto-generated method stub
List<User> list = userDao.queryUsers();
return list;
}
}
6.dao编写 工具类:JDBCUtils:
特点:Object...params
package cn.itcast.dao.Impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import cn.itcast.utils.JDBCUtil;
public class UserDaoImpl implements UserDao {
public Boolean findUser(User user) {
String sql = "select * from user where username=? and password=?;";
List<Map<String, Object>> Alist = JDBCUtil.query(sql, user.getUsername(),user.getPassword());
if(Alist.size()==0){
return false;
}
return true;
}
public List<User> queryUsers() {
// TODO Auto-generated method stub
String sql = "select * from user";
List<Map<String, Object>> Alist = JDBCUtil.query(sql);
if(Alist.size()==0){
return null;
}
List<User> list = new ArrayList<User>();
for (Map<String, Object> map : Alist) {
User user = new User();
user.setId((Integer)map.get("id"));
user.setUsername((String)map.get("username"));
user.setPassword((String)map.get("password"));
list.add(user);
}
return list;
}
}
7.jsp页面 (1)登录页面
<%@ 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>
</head>
<body>
<fieldset style="text-align: center;">
<form action="userLogin" method="post">
<span style="color:red;">${msg }</span>
<legend align="top">登录</legend>
用户名:<input name="user.username"/><br/>
密码:<input name="user.password" type="password"/>
<br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</fieldset>
</body>
</html>
(2)列表页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<style type="text/css">
.s1{
background-color: red;
}
.s2{
background-color: green;
}
</style>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>用户列表</h1>
<table cellpadding="20px" cellspacing="0px" bgcolor="yellow" width="100%" border="1px">
<tr>
<td>用户编号</td>
<td>用户名称</td>
<td>用户密码</td>
</tr>
<c:forEach items="${users }" var="u" varStatus="s">
<tr>
<td>${u.id }</td>
<td>${u.username }</td>
<td>${u.password}</td>
</tr>
</c:forEach>
</table>
<a href="${pageContext.request.contextPath }/upload.jsp">文件上传页面</a>
</body>
</html>
(3)成功页面(略)
8.struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" extends="struts-default" namespace="/">
<!-- 拦截器 -->
<interceptors>
<interceptor name="LoginInterceptor" class="cn.itcast.web.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 全局 -->
<global-results>
<result name="login">/login.jsp</result>
<result></result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<!-- ########################################################################### -->
<action name="userLogin" class="cn.itcast.web.action.UserLoginAction" method="execute">
<result type="redirectAction">list</result>
<result name="fail">/login.jsp</result>
</action>
<action name="list" class="cn.itcast.web.action.UserAction" method="execute">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<result>/list.jsp</result>
</action>
</package>
</struts>