1.普通package
<!-- package元素: * name:为当前包起名 作用,用于继承 * namespace:命名空间,相当于一个房间号:我们需要对号入座 * extends:当使用action的时候,必须继承struts-default,如果不继承,struts2定义的所有方法将失效--> <package name="primer" namespace="/primer" extends="struts-default"> <!-- name:action路径 如果在当前命名空间下找不到action,默认执行执行action, 作用:在项目出现异常的时候,统一跳转的页面 --> <default-action-ref name="helloWorldAction"></default-action-ref> <!-- action元素: * name:页面需要处理页面访问的链接,即action路径 * class:处理当前链接的类(全路径)--> <action name="helloWorldAction" class="cn.itcast.web.a_primer.HelloWorldAction"> <!-- result元素: name:返回值(与XXXXXActon类中的return的返回值对应) /primer/success.jsp,需要跳转的页面 --> <result name="success">/primer/success.jsp</result> </action> <!-- 没有指定action中的class,默认是ActionSupprot中的execute()方法 如果没有指定result中的name,默认是执行success --> <action name="actionNoClass"> <result> /primer/formSuccess.jsp </result> </action> </package>
2.package 设置转发与重定向的写法
<package name="resulttype" namespace="/resulttype" extends="struts-default"> <action name="resulttypeAction" class="cn.itcast.web.c_resulttype.ResulttypeAction"> <!-- 写法1 --> <!-- <result name="success" type="dispatcher"> /resulttype/success.jsp </result> --> <!-- 写法2 --> <!-- <result name="success" type="dispatcher"> <param name="location">/resulttype/success.jsp</param> </result>--> <!--<result name="success" type="redirect"> /resulttype/success.jsp </result> --> <!-- 重定向到另一个资源的action --> <result name="success" type="redirectAction"> <!-- name="actionName":需要访问的action的路径 name="namespace":指向另一个Action资源的命名空间 如果使用重定向,request作用域将无效--> <param name="actionName">helloWorldAction</param> <param name="namespace">/primer</param> </result> </action> </package>
3.通配符在struts.xml配置中的使用
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 是否调用action中的动态方法:true(默认):允许调用(在action中可以!号) false:不允许调用调用动态方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="pattern" namespace="/pattern" extends="struts-default"> <!-- 定义全局的返回结果 --> <global-results> <result name="delete"> /pattern/successGlobal.jsp </result> </global-results> <!-- method:表示BookAction类中方法的名称,返回值String --> <action name="bookAction" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> <!-- 通配符实例(1) <a href="${pageContext.request.contextPath}/pattern/a_add.action"> 通配符映射示例(1)</a><br> <a href="${pageContext.request.contextPath}/pattern/b_add.action"> 通配符映射示例(1)</a><br> <a href="${pageContext.request.contextPath}/pattern/c_add.action"> 通配符映射示例(1)</a><br> --> <!--<action name="a_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="b_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="c_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> --> <!-- <action name="*_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> --> <!-- 通配符实例(2) <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action"> 图书</a><br> <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户</a><br> --> <!-- <action name="BookAction_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="UserAction_add" class="cn.itcast.web.d_pattern.UserAction" method="add"> <result name="add"> /pattern/UserAction.jsp </result> </action>--> <!-- <action name="*_add" class="cn.itcast.web.d_pattern.{1}" method="add"> <result name="add"> /pattern/{1}.jsp </result> </action> --> <!-- 通配符实例(3) <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br> <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户添加</a><br> <a href="${pageContext.request.contextPath}/pattern/UserAction_delete.action">用户删除</a><br> --> <!-- <action name="BookAction_add" class="cn.itcast.web.d_pattern.BookAction" method="add"> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="BookAction_delete" class="cn.itcast.web.d_pattern.BookAction" method="delete"> <result name="delete"> /pattern/success.jsp </result> </action> <action name="UserAction_add" class="cn.itcast.web.d_pattern.UserAction" method="add"> <result name="add"> /pattern/UserAction.jsp </result> </action> <action name="UserAction_delete" class="cn.itcast.web.d_pattern.UserAction" method="delete"> <result name="delete"> /pattern/success.jsp </result> </action>--> <!-- <action name="*_*" class="cn.itcast.web.d_pattern.{1}" method="{2}"> <result name="{2}"> /pattern/{1}.jsp </result> <result name="delete"> /pattern/success.jsp </result> </action> --> <!-- 动态方法调用 <a href="${pageContext.request.contextPath}/pattern/BookAction!add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction!delete.action">图书删除</a><br> --> <!-- <action name="BookAction" class="cn.itcast.web.d_pattern.BookAction"> <result name="add">/pattern/BookAction.jsp</result> <result name="delete">/pattern/success.jsp</result> </action>--> <!-- 使用通配符调用(在项目中建议使用) 使用通配符定义action<br> <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br> --> <action name="BookAction_*" class="cn.itcast.web.d_pattern.BookAction" method="{1}"> <result name="add">/pattern/BookAction.jsp</result> <result name="delete">/pattern/success.jsp</result> </action> </package> </struts>
4.上传文件功能的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.multipart.maxSize" value="5229339"></constant> <package name="upload" namespace="/upload" extends="struts-default"> <!-- 1、配置上传文件大小 2、配置允许上传文件的类型 3、配置允许上传文件的扩展名 --> <!-- <interceptors> <interceptor-stack name="defaultStack"> 将fileUpload放置到默认栈的后面,当默认栈执行完毕后,再执行 <interceptor-ref name="fileUpload"> <param name="maximumSize">5229339</param> <param name="allowedTypes">application/pdf,application/vnd.ms-excel</param> <param name="allowedExtensions">.pdf,.txt</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> --> <action name="uploadAction_*" class="cn.itcast.web.f_upload.UploadAction" method="{1}"> <interceptor-ref name="defaultStack"> <!-- 定义文件的大小,默认是2M --> <param name="fileUpload.maximumSize">1229339</param> <!-- 定义允许上传的文件类型,如果文件类型定义多个,需要用逗号分开 --> <param name="fileUpload.allowedTypes">application/pdf,application/vnd.ms-excel,application/vnd.ms-powerpoint</param> <!-- 定义文件的扩展名,如果存在多个扩展名,用逗号分开 --> <param name="fileUpload.allowedExtensions">.pdf,.ppt</param> </interceptor-ref> <result name="success">/upload/success.jsp</result> <!--input: 如果该类中上传文件失败,可跳转到/upload/error.jsp --> <result name="input">/upload/error.jsp</result> </action> <action name="uploadsAction_*" class="cn.itcast.web.f_upload.UploadsAction" method="{1}"> <result name="success">/upload/success.jsp</result> <result name="input">/upload/error.jsp</result> </action> </package> </struts>
5.自定义拦截器 配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="aop" namespace="/aop" extends="struts-default"> <!-- 自定义拦截器 --> <interceptors> <interceptor name="person" class="cn.itcast.web.g_aop.PersonInterceptor"></interceptor> <interceptor-stack name="personStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- 将自定义的拦截器放置到默认栈的后面 --> <interceptor-ref name="person"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 默认执行的拦截器,即自定义的拦截器 --> <default-interceptor-ref name="personStack"></default-interceptor-ref> <action name="userAction_*" class="cn.itcast.web.g_aop.UserAction" method="{1}"> <result name="success">/aop/success.jsp</result> <!-- 表示失败 --> <result name="error">/aop/error.jsp</result> </action> </package> </struts>
6.表单校验 配置
(1)手工校验的xml配置(仅需要指定input跳转页面即可)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="validate" namespace="/validate" extends="struts-default"> <action name="validateAction_*" class="cn.itcast.web.h_validate.ValidateAction" method="{1}"> <result name="success">/validate/success.jsp</result> <!-- 表示失败,input:如果验证失败需要跳转到的页面(struts2自带) --> <result name="input">/validate/login.jsp</result> </action> <action name="validateXmlAction_*" class="cn.itcast.web.h_validate.ValidateXmlAction" method="{1}"> <result name="success">/validate/success.jsp</result> <!-- 表示失败,input:如果验证失败需要跳转到的页面(struts2自带) --> <result name="input">/validate/loginxml.jsp</result> </action> </package> </struts>
(2)xml校验 (针对ValidateAction中的所有方法)(注意xml文件命名规则:ValidateXmlAction-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"> <!-- 使用XML文件对指定Action类做校验 --> <validators> <!-- 指定校验的字段名称,对应表单中name的属性值 --> <field name="username"> <!-- 针对某一字段的验证规则 struts2将验证规则存放到xwork-2.x.jar中 com.opensymphony.xwork2.validator.validators下的default.xml文件 --> <!-- 用户名不能为空 --> <field-validator type="requiredstring"> <!-- 指定验证规则中属性名称指定相应值 --> <param name="trim">true</param> <!-- 填写提示的错误信息 --> <message>用户名不能为空</message> </field-validator> </field> <field name="psw"> <!-- 密码不能为空 --> <field-validator type="requiredstring"> <param name="trim">true</param> <message><![CDATA[密码不能为空]]></message> </field-validator> <!-- 密码长度在6到12之间 --> <field-validator type="regex"> <param name="expression">^[A-Za-z0-9]{6,12}$</param> <message><![CDATA[密码的长度不匹配(需要输入6到12之间)]]></message> </field-validator> </field> </validators>
(3)xml校验(针对ValidateAction中的指定方法) (注意xml文件命名规则:ValidateXmlAction-validateXmlAction_login-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"> <!-- 使用XML文件对指定Action类做校验 --> <validators> <!-- 指定校验的字段名称,对应表单中name的属性值 --> <field name="username"> <!-- 针对某一字段的验证规则 struts2将验证规则存放到xwork-2.x.jar中 com.opensymphony.xwork2.validator.validators下的default.xml文件 --> <!-- 用户名不能为空 --> <field-validator type="requiredstring"> <!-- 指定验证规则中属性名称指定相应值 --> <param name="trim">true</param> <!-- 填写提示的错误信息 --> <message>用户名不能为空</message> </field-validator> </field> <field name="psw"> <!-- 密码不能为空 --> <field-validator type="requiredstring"> <param name="trim">true</param> <message><![CDATA[密码不能为空]]></message> </field-validator> <!-- 密码长度在6到12之间 --> <field-validator type="regex"> <param name="expression">^[A-Za-z0-9]{6,12}$</param> <message><![CDATA[密码的长度不匹配(需要输入6到12之间)]]></message> </field-validator> </field> <!-- 验证年龄不能小于0 --> <field name="age"> <field-validator type="agevalidator"> <message><![CDATA[年龄不能小于0]]></message> </field-validator> </field> </validators>
(4)自定义校验 ( 在src的目录下创建validators.xml,配置如下)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"> <validators> <!-- name:为自定义的校验规则指定名称, 与ValidateXmlAction-validateXmlAction_login-validation.xml文件中校验规则type的值对应 class:校验规则的类路径 --> <validator name="agevalidator" class="cn.itcast.web.h_validate.AgeValidator"></validator> </validators>
7.国际化 配置
<!-- 加载国际化的资源文件 name:在项目配置国际化资源文件,让显示的错误信息为中文 value:如果在src的目录下,直接填写properties文件的名称 如果将资源文件放置到cn.itcast.web下,此时填写cn.itcast.web.资源文件名称 --> <constant name="struts.custom.i18n.resources" value="fileupload,cn.itcast.web.i_i18n.resource,cn.itcast.web.l_model.token"></constant>
8.token 防表单重复提交 配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="model" namespace="/model" extends="struts-default"> <interceptors> <interceptor-stack name="tokenStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- 将token拦截器放置到默认栈的后面 --> <interceptor-ref name="token"> <!-- 针对Action中某个方法控制表单重复提交 ,需要添加includeMethods,指定Action的方法,如果是多个方法用逗号分开--> <param name="includeMethods">save</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="tokenStack"></default-interceptor-ref> <action name="customerAction_*" class="cn.itcast.web.l_model.CustomerAction" method="{1}"> <result name="add">/model/add.jsp</result> <result name="save">/model/success.jsp</result> <result name="edit">/model/edit.jsp</result> <!-- 定义表单重复提交 invalid.token:表示如果表单重复提交,需要跳转的页面--> <result name="invalid.token">/model/message.jsp</result> </action> </package> </struts>
时间: 2024-11-03 21:54:40