Struct2
Struts.xml配置文件内容(name属性值不能相同 <package name=”modell” extends=”struts-default”></package>直接继承 <package name=””extends=”modell”></package>间接继承 2. 命名空间(区分不同命名空间下的action) <package name=”modell” extends=”struts-default” namespace=”/user”></package> <result name=”test”>test.jsp</result>(在webcontent文件夹下新建文件夹user) <result name=”test”>/test.jsp</result>(没有新建文件夹,还是在webcontent下) 3. 将不同层级下的xml集合到struts.xml中(如果带有命名空间,访问的时候url要带有命名空间) <include file="/wyn/struts/day01/test.xml"/> 4. action的配置方式 4.1 指定action,指定method方法 4.2 dmi的一种配置方式url中action!method(action名!+方法名)(这种方式有安全漏洞,2.3版本以上不支持,需要开启)开启代码:<!-- DMI配置,因为该配置有安全漏洞,在2.3以上版本默认DMI被禁 用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 4.3 使用通配符的方式配置method 1 方法名+action名(url中的输入),其中在xml中action的名字要变成*+action 名字,并且要在action的标签中加入method属性 例如method=”{1}” 举例:<action name="*testAction" class="wyn.struts.day01.Test1" method="{1}" > <result name="test1">test4.jsp</result> </action> 2 通过匹配,调用不同action的execute()方法 举例:<action name="*testAction" class="wyn.struts.day01.{1}" > <!--*代表javebean={1}--> <result name="test1">test4.jsp</result> <result name="test">test1.jsp</result> </action>
6 ognl表达式(值栈就是在acction定义的集合(map,list,set))以及声明的变量,并且为这些集合和变量设置set/get方法 6.1对迭代器的使用
<%@ taglib uri="/struts-tags" prefix="s" %> 在struts2中使用s标签 <s:iterator value="books" id="book"><br> <s:property value="#book.title"/><br> <s:property value="#book.ad"/><br> </s:iterator>6.2 对if-else的使用(主要用于权限的控制)
<s:iterator value="books" id="book"><br> <s:if test="#book.a==o">o权限</s:if> <s:elseif test="#book.a==1">1权限</s:elseif> <s:else>无限权限</s:else> </s:iterator>6.3 struts2种iterator详细用法 S:iterator有3个属性 value:被迭代的集合,id:指集合中元素的id,status:迭代元素的索引 举例:<s:iterator value="books" id="bookId" status="st"> <s:property value="bookId"/><br> <s:property value="#bookId.a"/><br> <s:property value="#st.Index"/><br> <s:property value="#st.Count"/><br> </s:iterator>
7 拦截器的2种配置方式(包内定义拦截器,可以定义全局拦截器或者action内的拦截器) 7.1 继承Interceptor接口 7.7 继承AbstractInterceptor类,重写interceptor()方法 此方法更可行,实际上AbstractInterceptor类也是实现了Interceptor接口 Invocation.invoke();表示该方法执行完后执行action的execute()方法或者下一个拦 截器 7.3 方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法 MethodFilterInterceptor实现方法过滤中用到的2个参数 execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用”,”隔开(支持通配符,例如add,表示所有以add开头的方法),如果指定了这个参数拦截器不会拦截指定列表中的方法,就是所谓的黑名单 IncludeMethods:该参数指定拦截器需要拦截的方法列表,多个方法用”,”隔开(支持通配符,例如add,表示所有以add开头的方法),如果指定了这个参数,则指定的action在执行前会被拦截,就是所谓的白名单 举例: 在struts.xml配置文件中关于上述execludeMethods,IncludeMethods有2种实现方式,一种相当于全局,一种相当于局部 定义:<interceptor name="myMethodFilterInterceptor"class="wyn.struts.day01.interceptor.myInterceptor"> <param name=”includeMethods”>method1(方法名)</param> </interceptor> 使用(局部): <interceptor-ref name="myMethodFilterInterceptor1"></interceptor-ref> 8 action中的方法在抛异常的情况下需要配置相应的接收结果input.即要定义一个;如果不抛出异常,则不需要定义。因为action已经是最外层了,在向外抛异常,异常就无法处理 9 全局变量结果设置
<global-results> <result name=”error”>error.jsp</result> </global-results>10 nterceptor中使用session等其他
HttpSession session=ServletActionContext.getRequest().getSession(); String path=ServletActionContext.getServletContext().getRealPath();//获取路径11 返回值为SUCCESS时。在action的result中,name可以省略。举例a.jsp
12 result的属性值 type 12.1 type=”dispatcher”(默认) 转发到jsp页面(带有参数) 12.2 type=”chain” 跳转到action(带有参数) 12.3 type=”redirect” 跳转到jsp页面(不带参数),如果想传参数,需要url?参数名=参数值 在xml文件中,使用重定向转发传参的时候,如果参数的数量很多,不能直接使用&符 号来连接参数,因为xml无法识别,需要&进行转译;跳转到action时要带后 缀”.action” 12.4 type=”redirectAction” 跳转到action时不需要带后缀
13 truts2的运行流程 http请求–>web.xml配置文件(配置FilterDistpatcher拦截器)–>action代理–>action–>result(4种)–>jsp页面
14 上传文件 框架本身的限制,如果超出,则不会报错。如果在框架限制范围内,在自己定义的限制范围外,则会报错 14.1 form表单的格式
<form action="uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="file" > <input type="submit" name="提交"> </form>14.2 Action的内容
private File file; private String fileFileName; private String fileContentType;//文件类型(定义3变量,并且设置get,set方法) public String execute(){ String path=ServletActionContext.getServletContext().getRealPath("/upload"); System.out.println(path.toString()); //System.out.println( fileContentType.toString()); File destFile=new File(path,fileFileName); try { FileUtils.copyFile(file, destFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }14.3 拦截器限制上传文件的格式与大小
<interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,application/octet-stream </param> <param name="maximumSize">100000</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref>15 文件的下载 15.1 action的内容
private InputStream fileInputStream; private String fileName;(还要get/set方法,方便struts2.xml中下载action的配置) public String execute(){ try { fileInputStream=new FileInputStream("E:/shopStore项目问题.docx"); File file=new File("E:/test.doc"); fileName=file.getName(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; }Struts.xml文件的配置
<action name="FileUpload" class="wyn.struts.action.FileUpload"> <result type="stream"> <!--指定被下载文件的入口输入流--> <param name="inputName">fileInputStream</param> <!-- 指定下载的文件名 --> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <!-- 指定被下载文件的文件类型 <param name="contentType">application/octet-stream</param>--><!-- 默认为 text/plain--> <param name="contentType">application/octet-stream</param> </result> </action>16 拦截器栈时配置在标签内的,在使用拦截器栈的时候要包含系统本身自带的拦截器栈 举例:
<interceptor-stack name="myStack1"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor1"></interceptor-ref> </interceptor-stack>一般在使用拦截器的时候,在标签之后使用 举例:
<result name="fail">fail.jsp</result> <interceptor-ref name="myStack1"></interceptor-ref>17 数据校验 jsp页面
<s:debug> <form action="LoginAction" method="post"> 账号:<input type="text" name="user.username"> <span><s:property value="fieldErrors['error'][0]"/></span><br> 密码:<input type="text" name="user.password"> <span><s:property value="fieldErrors['error_pwd'][0]"/></span><br> <input type="submit" value="提交"> </form>Action页面
public void validate() { if(user.getUsername().trim()==null||user.getUsername().equals("")){ addFieldError("error","用户名不能为空"); }else if(user.getUsername().trim().length()<6){ addFieldError("error","用户名长度不能小于6"); } if(user.getPassword().trim()==null||user.getPassword().equals("")){ addFieldError("error_pwd","密码不能为空"); }else if(user.getPassword().trim().length()<6){ addFieldError("error_pwd","密码长度不能小于6"); } }18 利用@SkipValidation可以跳过对某个方法的校验 19 校验器 19.1非字段校验器 利用xml配置文件对jsp页面传参字段值进行验证 (注意1 xml文件的命名格式action名字-validation.xml 2 xml文件的路径(要在对应的action的目录下 ) 举例: jsp
账号:<input type="text" name="user.username"> <span><s:property value="fieldErrors['user.username'][0]"/></span><br> LoginAction-validation.xml文件 <validators> <validator type="requiredstring"> <param name="fieldname">user.username</param> <message>用户名不能为空</message> </validator> //email验证 <!-- <validator type="email"> <param name="fieldname">emailAddr</param> <message>emailAddr address not valid.</message> </validator> --> //正则表达式 <!-- <validation type="regex"> <param name="fieldname">phone</param> <param name="regex"><![CDATA[\d{3}-\d{3}-\d{3}]]></param> <message>手机格式必须为999-999-999</message> </validation> --> </validators>19.2字段校验
<field name=”phone”> <field-validator type=”requiredstring”> <message>手机号不能为空</message> </field-validator> </field>