标签:
public class PermissionInterceptor implements Interceptor { private static final long serialVersionUID = -5178310397732210602L; public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("进入拦截器"); if(session里存在用户){ String result = invocation.invoke(); }else{ return “logon”; } //System.out.println("返回值:"+ result); //return result; } }
<package name="itcast" namespace="/test" extends="struts-default"> <interceptors> <interceptor name=“permission" class="cn.itcast.aop.PermissionInterceptor" /> <interceptor-stack name="permissionStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name=" permission " /> </interceptor-stack> </interceptors> <action name="helloworld_*" class="cn.itcast.action.HelloWorldAction" method="{1}"> <result name="success">/WEB-INF/page/hello.jsp</result> <interceptor-ref name="permissionStack"/> </action> </package>
package com.dzq.action; public class HelloWorldAction { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String addUI(){ this.message="addUI"; return "success"; } public String execute() { this.message="execute"; return "success"; } }
package com.dzq.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class PermissionInterceptor implements Interceptor { @Override public void destroy() { } @Override public void init() { } @Override public String intercept(ActionInvocation invocation) throws Exception { Object user=ActionContext.getContext().getSession().get("user"); if(user!=null){ return invocation.invoke();//如果user不为空,代表用户已经登录,允许执行action中的方法 } ActionContext.getContext().put("message", "对不起,请先登录"); return "success"; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 指定访问后缀 --> <constant name="struts.action.extension" value="do,action"/> <package name="department" namespace="/test/department" extends="struts-default"> <interceptors> <interceptor name="permission" class="com.dzq.interceptor.PermissionInterceptor"/> <!--拦截器栈 --> <interceptor-stack name="permissionStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="permission"/> </interceptor-stack> </interceptors> <!-- 包内所有action都使用默认拦截器 --> <!-- <default-interceptor-ref name="permissionStack"/> --> <global-results> <result name="success">/WEB-INF/page/message.jsp</result> </global-results> <!--使用通配符访问 --> <action name="list_*" class="com.dzq.action.HelloWorldAction" method="{1}"> <interceptor-ref name="permissionStack"/> <!-- <interceptor-ref name=""></interceptor-ref> --> </action> </package> </struts>
<%@ 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> ${message}<br> </body> </html>
<%@ 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> <% request.getSession().setAttribute("user", "dudu"); %> 用户已登录 </body> </html>
<%@ 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> <%request.getSession().removeAttribute("user"); %> 用户退出登录 </body> </html>
流程:直接访问localhost:8080.../test/department/list_addUI.do,报用户尚未登录信息,访问user.jsp,设置用户登录session,再访问action,则显示已登录,访问logout.jsp销毁session,再访问action,则显示未登录,通过拦截器实现,拦截器在struts.xml中注册后使用,拦截action
public void validate() { if(this.mobile==null || "".equals(this.mobile.trim())){ this.addFieldError("username", "手机号不能为空"); }else{ if(!Pattern.compile("^1[358]\\d{9}").matcher(this.mobile.trim()).matches()){ this.addFieldError(“mobile", "手机号的格式不正确"); } } }
<result name="input">/WEB-INF/page/addUser.jsp</result>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!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>输入校验</title> </head> <body> <div align="center"> <h1>struts2输入校验</h1><hr> <s:fielderror/> <form action="${pageContext.request.contextPath }/person/manage_update.do" method="post"> 用户名:<input type="text" name="username"/><br> 手机号:<input type="text" name="mobile"/><br> <input type="submit" value="验证"/> </form> </div> </body> </html>
<%@ 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>结果</title> </head> <body> ${message } </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 指定访问后缀 --> <constant name="struts.action.extension" value="do,action"/> <package name="department" namespace="/person" extends="struts-default"> <action name="manage_*" class="com.dzq.action.PersonAction" method="{1}"> <result name="message">/WEB-INF/page/message.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
package com.dzq.action; import java.util.regex.Pattern; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport{ private String username; private String mobile; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String update(){ ActionContext.getContext().put("message", "更新成功"); return "message"; } public String save(){ ActionContext.getContext().put("message", "保存成功"); return "message"; } /** * 输入校验,对action中所有方法进行校验 */ @Override public void validate() { if(this.username==null||"".equals(this.username.trim())){ this.addFieldError("username","用户名不能为空"); } if(this.mobile==null||"".equals(this.mobile.trim())){ this.addFieldError("mobile","手机号不能为空"); }else{ if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){ this.addFieldError("mobile","手机号格式不正确"); } } } }
2.手工编写代码实现对action中指定方法输入校验
public String add() throws Exception{ return "success";} public void validateAdd(){ if(username==null && "".equals(username.trim())) this.addFieldError("username", "用户名不能为空"); }
<result name="input">/WEB-INF/page/addUser.jsp</result>
/* * 只会对update方法输入校验 */ public void validateUpdate() { if(this.username==null||"".equals(this.username.trim())){ this.addFieldError("username","用户名不能为空"); } if(this.mobile==null||"".equals(this.mobile.trim())){ this.addFieldError("mobile","手机号不能为空"); }else{ if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){ this.addFieldError("mobile","手机号格式不正确"); } } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator> </field> </validators>
import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport{ private String username; private String mobile; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String update(){ ActionContext.getContext().put("message", "更新成功"); return "message"; } public String save(){ ActionContext.getContext().put("message", "保存成功"); return "message"; }
}
PersonAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"><!-- 指定校验器 --> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator> </field> <field name="mobile"> <field-validator type="requiredstring"><!-- 指定校验器 --> <param name="trim">true</param> <message>手机号不能为空!</message> </field-validator> <field-validator type="regex"> <!-- 指定正则校验器--> <param name="expression"><![CDATA[^1[358]\d{9}$]]></param> <message>手机号格式不正确!</message> </field-validator> </field> </validators>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 指定访问后缀 --> <constant name="struts.action.extension" value="do,action"/> <package name="department" namespace="/person" extends="struts-default"> <action name="manage_*" class="com.dzq.action.PersonAction" method="{1}"> <result name="message">/WEB-INF/page/message.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
<field-validator type="required"> <message>性别不能为空!</message> </field-validator>
<field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator>
<field-validator type="stringlength"> <param name="maxLength">10</param> <param name="minLength">2</param> <param name="trim">true</param> <message><![CDATA[产品名称应在2-10个字符之间]]></message> </field-validator>
<field-validator type="email"> <message>电子邮件地址无效</message> </field-validator>
<field-validator type="regex"> <param name="expression"><![CDATA[^1[358]\d{9}$]]></param> <message>手机号格式不正确!</message> </field-validator>
<field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年龄必须在1-150之间</message> </field-validator>
<field name="imagefile"> <field-validator type="fieldexpression"> <param name="expression"><![CDATA[imagefile.length() <= 0]]></param> <message>文件不能为空</message> </field-validator> </field>
<action name="user_*" class="cn.itcast.action.UserAction" method="{1}“ > <result name="success">/WEB-INF/page/message.jsp</result> <result name="input">/WEB-INF/page/addUser.jsp</result> </action>
public String add() throws Exception{ .... } public String update() throws Exception{ .... }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"><!-- 指定校验器 --> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator> </field> <field name="mobile"> <field-validator type="requiredstring"><!-- 指定校验器 --> <param name="trim">true</param> <message>手机号不能为空!</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[^1[358]\d{9}$]]></param> <message>手机号格式不正确!</message> </field-validator> </field> </validators>
<action name="user" class="cn.itcast.action.UserAction" method="{1}"> </action>
标签:
原文地址:http://www.cnblogs.com/xiaoduc-org/p/5450806.html