Struts2(四)拦截器—Java基础篇

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>StrutsDemo</display-name>


<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>    


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

2.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>


<!--bean 标签用于创建一个JavaBean实例 -->
<!--constant 标签用于默认行为 -->
<!--package 包标签用于区分不同的请求,比如网站前台请求、网站后台请求 -->


<!--配置web默认编码集,相当于HttpServletRequest.setChartacterEncoding用法 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--默认我们struts2的请求后缀是.action,也就是说我们不配置该元素,action/do都可以 -->
<constant name="struts.action.extension" value="action,do"></constant>
<!--设置浏览器是否缓存静态内容,默认值是true,在我们开发阶段建议关闭,防止修改后测试失败 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!--当struts配置文件修改后,系统是否自动重新加载该文件,默认为false -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--开发模式下使用,这样可以打印出更加详细的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!--默认视图主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />


<!--name包名,用于被别的包调用或继承 namespace="" -->
<package name="uweb" extends="struts-default">
<interceptors>
<interceptor name="CheckInter" class="com.jike.interceptor.CheckInterceptor">
<!--定义属性 -->
<!-- <param name="someThing">admin</param> -->
</interceptor>


<!--方法拦截器  -->
<interceptor name="MethodInter"
class="com.jike.interceptor.MethodInterceptor">
<param name="includeMethods">add</param>
<!-- <param name="excludeMehods">show</param> -->
</interceptor>


<!--拦截方法栈 -->
<interceptor-stack name="UserInter">
<interceptor-ref name="CheckInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>


<!--自定义默认拦截器 -->
<default-interceptor-ref name="UserInter" />


<!--全局返回结果 -->
<global-results>
<result name="error">/login.jsp</result>
<result name="success">/success.jsp</result>
</global-results>


<!-- <global-allowed-methods>regex:.*</global-allowed-methods> -->
<!--action相当于以前的servlet的概念,对应一个请求name为请求地址的url地址 localhost:8080/项目名/user/login.do -->
<action name="login" class="com.jike.action.LoginAction">
<!--定义拦截器,这样就不会走 自定义默认拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/index.jsp</result>
<result name="error">/login.jsp</result>
</action>


<action name="*_*" class="com.jike.action.{1}" method="{2}">
<interceptor-ref name="MethodInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/success.jsp</result>
<allowed-methods>add,show,update,delete</allowed-methods>
</action>


<!-- <action name="UserAction" class="com.jike.action.UserAction" method="*"> 
<result name="success">/success.jsp</result> <allowed-methods>regex:.*</allowed-methods> 
</action> -->
</package>

</struts>

3.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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>
<form action="<%=path%>/login.action" method="post">
username: <input type="text" name="username" /><br /> password: <input
type="password" name="password" /><br /> <input type="submit"
value="submit" /> <input type="reset" value="reset" />
</form>
</body>

</html>

4.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>


<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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>
<h3>This is index.jsp</h3>
<h3>用户</h3>
<a href="<%=path%>/UserAction_add.action">add</a>
<br>
<a href="<%=path%>/UserAction_show.action">show</a>
<br>




<h3>新闻</h3>
<a href="<%=path%>/NewsAction_add.action">add</a>
<br>
<a href="<%=path%>/NewsAction_show.action">show</a>
<br>
</body>

</html>

4.success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>This is success.jsp</h3>
</body>

</html>

5.LoginAction.java

package com.jike.action;


import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;


public String execute() throws Exception {


if (username.equals("admin") && password.equals("123456")) {
ServletActionContext.getRequest().getSession().setAttribute("username", username);
return "success";
} else {
return "error";
}
}


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;
}

}

6.CheckInterceptor.java

package com.jike.interceptor;


import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.sun.net.httpserver.Authenticator.Success;


public class CheckInterceptor implements Interceptor {


/**

*/
private static final long serialVersionUID = -5836201303675971816L;


@Override
public void init() {
System.out.println("Interceptor init");
}


@Override
public String intercept(ActionInvocation arg0) throws Exception {
String url = "";
System.out.println("start Interceptor intercept");
if (null != ServletActionContext.getRequest().getSession().getAttribute("username")) {
/*url = arg0.invoke();*/
url = "success";
} else {
url = "error";
}
return url;
}


@Override
public void destroy() {
System.out.println("Interceptor destroy");
}


}

7.MethodInterceptor.java

package com.jike.interceptor;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;


public class MethodInterceptor extends MethodFilterInterceptor {


/**

*/
private static final long serialVersionUID = 7016796101531331454L;


@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
System.out.println("start MethodInterceptor doIntercept");
String url = arg0.invoke();
System.out.println("end MethodInterceptor doIntercept");
return url;
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值