Struts2---自定义拦截器
一:UserIfo
-
public class UserInfo {
-
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;
-
}
-
}
二:LoginAction
-
public class LoginAction implements Action {
-
private UserInfo user;
-
-
public String execute() throws Exception {
-
Map<String, Object> session = ActionContext.getContext().getSession();
-
if (user != null) {
-
if (user.getUsername().equals("1") && user.getUsername().equals("1")) {
-
session.put("uname", user.getUsername());
-
return SUCCESS;
-
} else {
-
return LOGIN;
-
}
-
} else {
-
if (session != null && session.get("uname") != null) {
-
return "success";
-
} else {
-
System.out.println("自定义代码");
-
return "login";
-
}
-
}
-
}
-
-
-
public UserInfo getUser() {
-
return user;
-
}
-
-
public void setUser(UserInfo user) {
-
this.user = user;
-
}
-
}
-
三:BookAction
-
public class BookAction implements Action {
-
public String execute() throws Exception {
-
System.out.println("bookaction");
-
return SUCCESS;
-
}
-
}
-
四:MyInterceptor
-
public class MyInterceptor implements Interceptor {
-
public void destroy() {
-
-
}
-
-
public void init() {
-
System.out.println("拦截器已经成功初始化.....");
-
}
-
-
public String intercept(ActionInvocation invocation) throws Exception {
-
System.out.println("对象" + invocation);
-
Object action = invocation.getAction();
-
System.out.println("action===========" + action);
-
String value;
-
Map<String, Object> session = ActionContext.getContext().getSession();
-
Object name = session.get("uname");
-
String actionName = invocation.getProxy().getActionName();
-
invocation.getProxy().getNamespace();
-
System.out.println("actionNmae=============" + actionName);
-
if (actionName.equals("login")) {
-
System.out.println("================");
-
value = invocation.invoke();
-
} else if (name != null) {
-
value = invocation.invoke();
-
String method = invocation.getProxy().getMethod();
-
System.out.println("方法名=========" + method);
-
} else {
-
value = "login";
-
}
-
System.out.println("逻辑视图名====" + value);
-
return value;
-
}
-
}
-
五:struts-day07-02.xml
-
<struts>
-
<constant name="struts.devMode" value="true"></constant>
-
<package name="day07" namespace="/" extends="default">
-
<action name="login" class="cn.day07interceptor02.action.LoginAction">
-
<result name="success">/WEB-INF/myinter/success.jsp</result>
-
<result name="login">/WEB-INF/myinter/login.jsp</result>
-
</action>
-
<action name="book" class="cn.day07interceptor02.action.BookAction">
-
<result name="success">/WEB-INF/myinter/booksuccess.jsp</result>
-
</action>
-
</package>
-
</struts>
六:struts.ml
-
<struts>
-
<constant name="struts.devMode" value="true"></constant>
-
<package name="day07" namespace="/" extends="default">
-
<action name="login" class="cn.day07interceptor02.action.LoginAction">
-
<result name="success">/WEB-INF/myinter/success.jsp</result>
-
<result name="login">/WEB-INF/myinter/login.jsp</result>
-
</action>
-
<action name="book" class="cn.day07interceptor02.action.BookAction">
-
<result name="success">/WEB-INF/myinter/booksuccess.jsp</result>
-
</action>
-
</package>
-
</struts>
-
<struts>
-
<!--支持动态方法调用-->
-
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
-
<!--修改struts.xml 不用重启容器-->
-
<constant name="struts.devMode" value="true"/>
-
<package name="default" namespace="/" extends="struts-default">
-
<interceptors>
-
<interceptor name="myInter" class="cn.happy.day07interceptor.interceptor.MyInterceptor"></interceptor>
-
<interceptor-stack name="myStack">
-
<interceptor-ref name="defaultStack"></interceptor-ref>
-
<interceptor-ref name="myInter"></interceptor-ref>
-
</interceptor-stack>
-
</interceptors>
-
<default-interceptor-ref name="myStack"></default-interceptor-ref>
-
<default-action-ref name="first"></default-action-ref>
-
<action name="first"></action>
-
<!--<global-results>
-
<result name="error">/WEB-INF/jsp/error.jsp</result>
-
</global-results>-->
-
<!-- <global-exception-mappings>
-
<exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>
-
</global-exception-mappings>-->
-
<action name="helloWord" class="cn.day01.action.TestAction01">
-
<result name="success">day01jsp/first.jsp</result>
-
</action>
-
</package>
-
<!--<include file="struts-day03.xml"></include>
-
<include file="struts-day05.xml"></include>
-
<include file="struts-day06.xml"></include>
-
<include file="struts-day04.xml"></include>
-
<include file="struts-day07.xml"></include>
-
<include file="struts-day08.xml"></include>-->
-
<include file="struts-day07-02.xml"></include>
-
</struts>
七:login.jsp
-
<body>
-
<h2>登录</h2>
-
<s:form method="POST" action="login">
-
请输入用户名: <s:textfield name="user.username"></s:textfield> <br/>
-
<s:textfield name="user.password"></s:textfield><br/>
-
<s:submit value="登陆"></s:submit>
-
</s:form>
-