Struts2学习笔记(未完待续)

    Struts2是在WebWork2基础发展而来的。和struts1同样,struts2也属于MVC框架。不过有一点须要注意的是:尽管struts2和struts1在名字上差异不是很大,可是struts2和struts1在代码编写风格上几乎是不同的。那么既然有了struts1,为什么还要推出struts2?主要是由于struts2有如下优势:                                                                     1>在软件设计上,struts2没有像struts1那样跟Servlet API和struts API有着紧密的耦合,struts2的应用能够不依赖Servlet API和struts API。Struts2的这种设计属于无侵入式设计,而struts1却属于侵入式设计。 html

1 public class OrderListAction extends Action{
2     public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
3         HttpServletResponse response) throws Exception{}
4   }

    2>Struts2提供了拦截器,利用拦截器能够进行AOP编程,实现如权限拦截等功能。
    3>Struts2提供了类型转换器,咱们能够把特殊的请求参数转换成须要的类型。在struts1中,若是咱们要实现一样的功能,就必须想struts1的底层实现BeanUtil注册类型转换器才行。
    4>Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等
    5>Struts2的输入校验能够对指定方法进行校验,解决了Struts1长久之痛。
    6>提供了全局范围、包范围和Action范围的国际化资源文件管理实现  java

搭建Struts2开发环境
搭建Struts2环境时,咱们通常须要作如下几个步骤的工做:(一下文件均可以在下载的struts2的文件夹中能够找到,找不到的能够联系我。QQ:434551893)
     1>找到开发Struts2应用须要使用到的jar文件,拷贝到WEB-INF下/lib目录下
     2>编写Struts2的配置文件struts2.xml
     3>在web.xml中加入Struts2 MVC框架启动配置
添加源码文件(创建jar文件对应的源码):右键点击以前添加的struts2-core-2.3.24.1.jar文件,选择Properties --> Java Source Attachment --> External Folder,而后选择源码位置struts-2.3.24.1\src\core\src\main\java
xwork2.3.3 源码:在xwork2.3.3.jar上右键--属性--java source Attachment,选择external folder把解压后的文件目录定位到Java便可,路径是xwork-core/src/main/java
添加文档文件:右键点击以前添加的struts2-core-2.3.24.1.jar文件,选择Properties --> Javadoc Location --> URL--Browse  找到struts-2.3.24.1\docs\struts2-core\apidocs OK!          
快速提示设置:先在本地解压一个lib文件夹下边struts2-core-2.3.24.1.jar文件,里边能够看到dtd文件。而后打开window --> Preferences --> 搜索catalog  选中XML Catalog  点击Add  Location:本地dtd文件位置,Key Type:选择URI,Key:struts2.xml配置文件中的http://struts.apache.org/dtds/struts-2.3.dtdweb

路径问题:struts2中的路径问题是根据action的路径而不是jsp路径来肯定,因此尽可能不要使用相对路径。虽然能够用redirect方式解决,但redirect方式并不是必要。解决办法很是简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径。例如:在jsp的<head>标签前边写上:<% String context = request.getContextPath(); %>,在<body>标签里面使用:<a href="<%=context %>/actions(actions是namespace)/Studentadd(这是action)">添加学生</a>)或者使用myeclipse常用的,指定basePath:<base href="<%=basePath%>"/>   apache

Struts2应用的配置文件
Struts2默认的配置文件为struts.xml,该文件须要存放在WEB-INF/classes下,在开发阶段能够放在src下就能够,该文件的配置模板以下(最基本的配置中<struts>标签里边为空就能够):编程

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true" />  <!-- 设置为开发模式,更改东西能够很快就会有反馈 -->     
 7     <package name="ghostman" namespace="/test" extends="struts-default">  //定义一个包来管理action。action访问路径为:namespace/+"action的name"             
 8         //com.ghostman.action包名,HelloWorldAction包中类名,execute是HelloWorldAction类中的execute()方法,而且返回值是String
 9         <action name="helloworld" class="com.ghostman.action.HelloWorldAction" method="execute">   //此action的访问路径为/test/helloworld.action
10             <result name="success">/WEB-INF/page/hello.jsp</result>  //执行该Action时会跳转到/WEB-INF/page下的hello.jsp
11         </action>
12         <action name="addUI">
13             <result>/WEB-INF/page/employeeAdd.jsp</result>
14         </action>
15     </package>
16 </struts>
struts2配置模板

Action配置中的各项默认值  api

  一、若是没有为action指定class,默认是ActionSupport。  app

  二、若是没有为action指定method,默认执行action中的execute()方法。  框架

  三、若是没有指定result的name属性,默认值为success。凡是name是success的result均可以不写name属性。 eclipse

Action执行的时候并不必定要执行execute方法,能够在配置Action的时候用method=""来指定执行哪一个方法,也能够在url地址中动态指定(动态方法调用DMI)(推荐)。 webapp

struts2框架中使用包来管理Action,包的做用和java中的类包是很是类似的,它主要用于管理一组业务功能相关的action。 在实际应用中,咱们应该把一组业务功能相关的Action放在同一个包下。其实包的做用其实就是为了防止重名。包还能够经过abstract="true"定义为抽象包,抽象包中不能包含action。

配置包时必须指定name属性,该name属性值能够任意取名,但必须惟一,它不对应java的类包,若是其余包要继承该包, 必须经过该属性进行引用。

包的namespace属性用于定义该包的命名空间,namespace必须以"/"开头,命名空间做为访问该包下Action的路径的一部分,如访问上例的Action, 访问路径为:/test/helloworld.action。 namespace最好也用模块来进行命名 namespace属性能够不配置,意味着namespace="",对本例而言,若是不指定该属性,默认的命名空间为""(空字符串)。

一般每一个包都应该继承struts-defaul包,由于struts2不少核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、 文件上传和数据验证等等都是经过拦截器实现的。 struts-defaul定义了这些拦截器和Result类型。能够这么说:当包继承了struts-defaul才能使用struts2提供的核心功能。 struts-defaul包是在struts2-core-2.x.x.jar文件中的struts-defaul.xml中定义。 struts-defaul.xml也是struts2默认配置文件。Struts2每次都会自动加载struts-defaul.xml文件。

拷贝其余项目应注意:拷贝其余struts2项目时,须要右键选中拷贝的项目名,选择Properties,找到MyEclipse下的web,必定把Web Context-root中的内容修改成你本身项目的名字,否则部署以后会找不到文件

通配符的使用:能够将配置降到最低,不过必定要遵照”约定优于配置“的原则。若是有多个<action>能够匹配,首先匹配最精确的。若是都是通配符,则谁在前边调用谁。举例以下:

StudentAction:

 1 package com.hp.struts2.action;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 public class StudentAction extends ActionSupport{
 4     public String add(){
 5         return SUCCESS;
 6     }
 7     public String delete(){
 8         return SUCCESS;
 9     }
10 }
StudentAction.java

TeacherAction:

 1 package com.hp.struts2.action;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 public class TeacherAction extends ActionSupport{
 4     public String add(){
 5         return SUCCESS;
 6     }
 7     public String delete(){
 8         return SUCCESS;
 9     }
10 }
TeacherAction.java

struts.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true" />   <!-- 设置为开发模式,更改东西能够很快就会有反馈 -->
 7     <package name="default" namespace="/actions" extends="struts-default">
 8         <action name="Student*" class="com.hp.struts2.action.StudentAction" method="{1}">
 9             <result>/Student{1}_success.jsp</result>
10         </action>    
11         <action name="*_*" class="com.hp.struts2.action.{1}Action" method="{2}">
12             <result>/{1}_{2}_success.jsp</result>
13         </action>    
14     </package>
15 </struts>
struts.xml

在WebRoot下面建立Studentadd_success.jsp、Studentdelete_success.jsp、Teacher_add.jsp、Teacher_delete.jsp
解释:针对第一个<action>,当接收到Studentadd和Studentdelete的action时,会自动匹配到Student*,此时*即add或者delete,{1}是第一个与*匹配的方法,即add()和delete()方法。针对第二个<action>,当接收到Teacher_add和Teacher_delete的action时,会自动匹配到第二个<action>,此时的前边的*就是{1},即Teacher,后边的*就是{2},即add或者delete。若是看不懂能够加我QQ:434551893一块儿交流交流

用Action的属性接收参数:Struts2中Action的属性接收参数,有三种传递并接收参数的方式,第一种是在Action添加成员属性接收参数,第二种是域模型(DoMainModel),就是利用对象域来进行传递和接收参数,第三种是ModelDriven接收参数。具体内容请参考:http://www.2cto.com/kf/201503/384769.html,http://www.cnblogs.com/bukudekong/archive/2012/03/29/2423064.html

相关文章
相关标签/搜索