玩了一天终于把struts2的intercepter搞明白了,有那么点小兴奋,呵呵。
下面把俺的成果分享一下:
1.在注册页面中(index.jsp)注册一下:
<%
session.setAttribute("user","lzw");
%>
2.在登录界面(login.jsp)登录:
<form action="login.action" method="post"> <table align="center"> <tr> <td> 用户名: </td> <td> <input type="text" name="userName" /> </td> </tr> <tr> <td> 密 码: </td> <td> <input type="password" name="password" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="登录" /> </td> </tr> </table> </form>
3.登录类:LoginAction.java
package com.lzw.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { System.out.println("开始执行execute()"); Map<String,Object> map = ActionContext.getContext().getSession(); String user = (String) map.get("user"); System.out.println("The user'name is ---------- "+user); return SUCCESS; } }
4.success.jsp页面
welcome ${userName} to struts2 world。
5.拦截器类:LoginInterceper.java
package com.lzw.intercepter; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class LoginIntercepter extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { Map<String, Object> session = invocation.getInvocationContext() .getSession(); String user = (String) session.get("user"); System.out.println("11---------------- " + user); if ("".equals(user)||null==user) { System.out.println("22---------------- "); return Action.INPUT; } else { System.out.println(" 33---------------- " + user); return invocation.invoke(); } } }
6.struts2配置文件struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<interceptors>
<interceptor name="authority" class="com.lzw.intercepter.LoginIntercepter" />
<interceptor-stack name="loginStack">
<interceptor-ref name="authority" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack" />
<action name="login" class="com.lzw.action.LoginAction">
<result>/success.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
</package>
</struts>
7.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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8.部署到tomcat服务器上,在地址栏上输入:http://localhsot:8080/test来注册,
再重新输入:http://localhost:8080/test/login.jsp
标签:String,struts2,例子,user,intercepter,import,com,public From: https://blog.51cto.com/u_16065168/6459274