struts2的那点小事

处理用户数据信息(三种):

       1.利用action类的属性接收用户输入

       2.利用领域对象接收用户输入(类似struts1中的ActionForm)

       3.使用ModelDriver模式接收用户输入

 

Action方法的特点:

       只要一个类中提供了一个名为execute返回类型为String的方法的类都被认为是一个Action类。

 

Struts2的配置文件:

       1. web.xml     web应用程序的核心描述文件,里面配置struts2的过滤器FilterDispatcher

       2. struts.xml    主配置文件

       3. struts.properties    属性文件(可以用来更改struts的默认配置信息,即可以更改default.properties文件中的信息,参照default.properties文件中的部分注释信息如下:

         ### Struts default properties
         ###(can be overridden by a struts.properties file in the root of the classpath) 覆盖方式即将default.properties文件中的key-value对复制到struts.properties文件中,保持key不变,该表value值为自己的期望值即可,例如将.action改成.do,在struts.properties文件中写入struts.action.extension=do,default.properties中为:strtus.action.extension=action),此文件必须放入src下即classpath下。

       4. struts-default.xml     struts2的默认配置(struts2-core-x.x.x.jar中自带的)

       5. struts-plugin.xml      struts2框架的插件配置文件

 

default.properties文件详解:

       此文件是struts2-core-x.x.x.jar中自带的,里面有struts的所有默认配置信息。

       struts.action.extension=action,,

       struts.configuration.xml.reload=false

       。。。

       这些东西基本不用说,自己看一下就知道什么意思了,只要想改变这些属性就将相应的key-value对放入struts.properties文件中保持key不变,改变value为相应的值即可。

 

struts-default.xml:

        此文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。这些拦截器是以key-value对的形式配置在struts-default.xml中,其中name是拦截器名字,就是后面使用该拦截器的引用点,value则指定拦截器的实现类。

 

struts.xml配置讲解:

        在web应用程序中我们都是使用部署描述符来初始化一些资源如servlet、过滤器、监听器等等,这个部署描述符就是那广为人知的web.xml。同样的,框架也使用一个配置文件来初始化它自己的资源,这些资源主要包括:

        1. 拦截器(Interceptor):对请求进行预处理和后加工。

        2. Action Classes:负责调用商业逻辑和数据访问层。

        3. Result:负责返回视图(view),如JSP页面等等。

 

struts-plugin.xml:

         1. struts2提供了类似于Eclipse的插件机制来扩展自身的功能。

         2. struts-plugin.xml 就是由插件使用的配置文件,该文件的结构和struts.xml相同,存放在插件的jar包中。

 

struts2与spring集成:

         1. 将struts2-spring-plugin-x.x.x.jar文件包含到我们的应用中,放到WEB-INF/lib目录下面即可。在这个插件包中有个struts-plugin.xml文件,它的内容如下:

<struts>

     <bean  type="com.opensymphony.xwork2.ObjectFactory"  name="spring"  class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

     <constant  name="struts.objectFactory"  value="spring" />

     <package  name="spring-default">

          <interceptors>

                  <interceptor  name="autowiring"  class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />

                  <interceptor  name="sessionAutowiring"  class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />

          </interceptors>

     </package>

</struts>

 

struts.xml配置---Bean配置

         1. struts2是个可以扩展的框架,框架的核心组件都是可以配置的,这些组件可以通过struts2自身的依赖注入容器来装配。开发时可以自己编写组件来扩展框架功能,然后通过bean元素来配置组件。

         2. 在struts-default.xml文件中定义了struts2框架中可配置的组件。

 

Bean的两种配置方法:

          1.  框架的IOC容器创建bean的实例,然后将该实例注入到框架的内部对象中。

            第一种做法可以称为对象注入,它通常要用到bean的type属性,告诉容器这个对象实现了哪个接口。如果自己创建了ObjectFactory,则可以在struts-default.xml中做如下配置:

<struts>

    <bean  type="com.opensymphony.xwork2.ObjectFactory"  name="struts"  class="org.apache.struts2.impl.StrutsObjectFactory"  />

</struts>

          2. 通过bean的静态方法向bean注入值。

            第二种做法使用值注入,允许不创建bean,而让bean接受框架常量。Bean使用值注入,必须使用static属性,并将该属性设置为true。struts-default.xml文件中可以做如下配置:

<bean  class="com.opensymphony.xwork2.ObjectFactory"  static="true"  />

 

常量配置:

       我们可以通过定义一些能够改变框架和插件行为的关键设置来定制我们的struts应用程序,而这些设置就是常量。常量扮演了两个关键的角色:

       1. 它们被用来覆盖一些原有的默认设置,例如上传文件的最大容量、框架是否处于开发模式等等;

       2. 常量还用来指定在一个类型(type)的多个实现中,哪个bean应该被选中。

       3. 常量可以定义在多个文件中,默认情况下我们按照下面的顺序来寻找常量,后面的将覆盖前面的设置:

           struts-default.xml

           struts-plugin.xml

           struts.xml

           struts.properties

           web.xml

           前3个xml文件的格式是一样的,因为它们使用同一个DTD文件,在xml文件中Constant元素有两个必须的属性:name和value:在struts.properties文件中,每一个entry都被视为一个常量;在web.xml文件中FilterDispatcher的初始化参数被载入为常量。

struts.xml中:

<struts>
 <constant name="struts.i18n.encoding" value="UTF-8" />
    <package name="com.simier.action.login" extends="struts-default">
     <action name="login" class="com.simier.action.LoginAction">
      <result name="loginSuccess">/loginSuccess.jsp</result>
      <result name="loginFail">/loginFail.jsp</result>
     </action>
    </package>
</struts>

 

struts.properties中:

struts.i18n.encoding=UTF-8

 

web.xml中:

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  <init-param>
   <param-name>struts.i18n.encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 

包配置:

       1. struts2中的包类似java中的包,提供了将action、result、拦截器和拦截器栈组织成一个逻辑单元的一种方式,从而为你简化了维护工作,提高了重用性。在struts.xml文件中,与web应用相关的设置都在包中定义,每一个包中都包含了将要用到的action、result、拦截器和拦截器栈。

 

命名空间:

        1. package元素的namespace属性可以将包中的action配置为不同的命名空间,这样就可以在不同的命名空间中使用同名action。struts2框架使用命名空间和action的名字来标识一个action。

        2. 默认的命名空间是空字符串"",也就是不设置namespace属性时候的命名空间。我们在匹配一个action的时候,先到它指定的命名空间中去找,如果没有再到这个默认的命名空间中去找。struts2还支持根命名空间("/"),当一个request直接请求context path下面的资源时,struts2会首先到根命名空间下去寻找匹配的action,理论如请求是http://127.0.0.1/mystruts2/xxx.action,那么我们首先回去"/"命名空间下去寻找这个action

 

struts.xml:

         在多人合作开发项目时,为了避免同一文件提交冲突带来的不便,我们可以将struts.xml文件分成多个子文件,在主文件中使用标签<include file="xxx.xml" />将所有的子文件包含进来即可。

 

借助命名空间实现简单的安全验证:

         1. 例如命名空间为namespace="/test"

web.xml中:

 <security-constraint>
  <web-resource-collection>
   <web-resource-name>my website auth</web-resource-name>
   <description>authentication anything that was included in the directory /test/</description>
   <url-pattern>/test/*</url-pattern> <!-- which directory need to be protected -->
   <http-method>GET</http-method>
   <http-method>POST</http-method>
  </web-resource-collection>
  <auth-constraint>
   <role-name>admin</role-name>
  </auth-constraint>
 </security-constraint>
 
 <security-role>
  <role-name>admin</role-name> <!-- there will be all roleName and above roleName must from here. -->
 </security-role>
 
 <login-config>
  <auth-method>BASIC</auth-method> <!-- authentication method. A total of four kinds of the authentication method -->
  <realm-name>basic auth</realm-name> <!-- The name you can enter any character -->
 </login-config>

 

tomcat目录下的\conf目录下tomcat-users.xml文件中:

<role rolename="admin" />

<user username="simier"  password="123"  roles="admin" />

 

拦截器:

        1. 在struts2中,许多action通常都有一些共同的需要关心的问题,比如有一些action它们都需要对页面上的输入进行验证,有一些action需要对上传的文件进行一下预处理,还有一些action可能需要防止重复提交(resubmit),还有很多action需要在页面显示之前将下拉列表或者其它的一些控件事先装好值。通过使用“拦截器”策略,struts2框架使得共享这些问题的解决方案变得十分容易。

例如使用struts2中的默认拦截器:

 <package name="com.simier.action.login" extends="struts-default">
     <!-- <interceptors>
      <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
      <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
     </interceptors> -->
     <action name="login" class="com.simier.action1.LoginAction">
      <result name="loginSuccess">/loginSuccess.jsp</result>
      <result name="loginFail">/loginFail.jsp</result>
      <interceptor-ref name="timer" />
      <interceptor-ref name="params" />
     </action>
    </package>

 

上面的两个拦截器我们可以在package标签下引入,也可以不写,因为我们已经集成了struts-default包,而这两个拦截器就在struts-default包下,所以我们只要直接使用标签<interceptor-ref>引入struts2提供的拦截器就可以使用了,如果我们自定义的包没有继承struts-default则上面注释的部分必须有,自定义包也可以相互继承。

 

Action配置:

         1. method方式访问:

          <action name="login" class="com.simier.action1.LoginAction" method="init">   此处method相当于struts1中的DispatcherServlet的功能,它将服务对应到Action中的响应方法,此处意义表示将login服务请求交给LoginAction中的init方法来做处理。

          2.  动态访问方式:

          在请求的服务后面用!分割,并附带上要访问的方法名,如下:

         struts.xml中: <action name="login" class="com.simier.action1.LoginAction">

          访问页面中:<a  href="<%path%>/login!init.action">   表示通过服务请求login访问LoginAction中的init方法。

 

两种访问方式的差异:

         method访问方式每个方法可以返回不同的结果,也可以使用不同的拦截器,因为有多个<action>配置并且每个<action>是独立的,而动态访问方式只适合所有方法都返回相同结果的情况,而且使用的拦截器也必须是相同的情况,因为动态访问方式只有一个<action>。

         使用method方式时Action类中的方法名可以是xxx或者doXxx,例如:

         struts.xml中:

          <action name="login" class="com.simier.action1.LoginAction" method="init">

         Action中:

         public  String  init(){}       或者    public  String  doInit(){}  都是可以的。

         3. ForwordAction方式:

         在struts.xml中加入如下内容:

                       <action name="login">
                              <result  type="redirect">/main.jsp</result>                此处type为跳转的方式,默认为服务器端跳转,如果没有参数传递就使用重定向即显示指定跳转方式
                       </action>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值