Struts2实现第一个简单拦截器
来源:互联网 发布:淘宝订购的应用 编辑:程序博客网 时间:2024/06/05 06:04
1:jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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%>">
<title>My JSP 'reg.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">
</head>
<body>
<h2>拦截器初步使用(进行离开拦截后验证)</h2><hr/>
<s:form action="helloaction" method="post">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="pass1" label="密码"></s:password>
<s:password name="pass2" label="确认密码"></s:password>
<s:submit value="注册"></s:submit>
</s:form>
</body>
</html>
2: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">
<display-name>StrutsTest1</display-name>
<!-- 配置struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
3: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.i18n.encoding" value="UTF-8"></constant>
<package name="struts2login" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success" >/result.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="input" class="com.test.action.InputAction">
<result name="success" >/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="message" class="com.test.action.InputAction">
<result name="success" >/message.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<package name="demo1" extends="struts-default">
<action name="addRegister" class="com.test.action.RegisterAction">
<result name="success">/showRegInfo.jsp</result>
</action>
<action name="addRegister1" class="com.test.action.Register1Action">
<result name="success">/showRegInfo1.jsp</result>
</action>
<action name="convert" class="com.test.action.ConvertAction">
<result name="success">/convertResult.jsp</result>
</action>
</package>
<package name="hellointerceptor" extends="struts-default">
<interceptors>
<interceptor name="helloInterceptor" class="com.test.action.HelloInterceptor">
</interceptor>
</interceptors>
<action name="helloaction" class="com.test.action.HelloAction">
<result name="success">/success1.jsp</result>
<result name="input1">/input1.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="helloInterceptor"></interceptor-ref>
</action>
</package>
</struts>
5;拦截器进行拦截
package com.test.action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class HelloInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("***拦截器开始执行***");
String resultString=arg0.invoke();
System.out.println("***拦截器执行完毕***");
return resultString;
}
}
6:Action
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String username;
private String pass1;
private String pass2;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass1() {
return pass1;
}
public void setPass1(String pass1) {
this.pass1 = pass1;
}
public String getPass2() {
return pass2;
}
public void setPass2(String pass2) {
this.pass2 = pass2;
}
public String execute() throws Exception
{
if((username!=null&&username!="")&&(pass1.equals(pass2)))
{
System.out.println("***验证正确,正在执行Action()方法***");
return "success";
}
else
{
System.out.println("***验证错误,正在执行Action()方法***");
return "input";
}
}
}
7:jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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%>">
<title>My JSP 'success1.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">
</head>
<body>
<h2>用户名:<s:property value="username"/></h2>
<h2>密 码:<s:property value="pass1"/></h2>
</body>
</html>
- Struts2实现第一个简单拦截器
- Struts2第一个拦截器
- struts2实现简单拦截器
- Struts2中拦截器的简单实现
- struts2实现简单action时间拦截器
- struts2 拦截器实现
- Struts2 自定义拦截器 实现简单权限检查
- struts2中拦截器的简单实现流程
- 简单的利用Struts2拦截器实现权限校验
- 自定义注解+Struts2拦截器实现简单权限控制
- Struts2拦截器简单示例
- Struts2拦截器简单示例
- Struts2拦截器简单模拟
- struts2拦截器 简单了解
- struts2拦截器简单例子
- struts2拦截器简单介绍
- 关于Struts2实现简单权限拦截功能
- struts2拦截器实现拦截不文明字迹
- 谁数学好,谁就是下一个编程大师!
- 杭电2028
- 最新微信暴利赚钱项目,微信充值附近聊天交友源码,内置机器人自动交流 带视频教程
- POJ 3170 线段树优化DP
- 如何计算换行符、空格与制表符的个数(以及为何不能用此法来算单词数)
- Struts2实现第一个简单拦截器
- oracle 11g client 和 PL/SQL developer的安装和配置
- 活动安排问题
- Problem 39 Integer right triangles (数学)
- VC6.0打开或者添加工程文件崩溃的解决方法
- 线程实现cp-r与ls-l
- HDU-1879-继续畅通工程
- 知识体系
- 在ls-l中利用execl添加cp-r进程