struts2使用要点

1. 在一个Action中包含多个处理逻辑,但是不管有多少个处理逻辑  public ,  String  ()

         1.1 动态方法调用DMI

                   1.1.1  ActionName!methodName

                   1.1.2  ActionName?method:methodName

                   ### Set this to false if you wish to disable implicit dynamic method invocation

                   ### via the URL request. This includes URLs like foo!bar.action, as well as params

                   ### like method:bar (but not action:foo).

                   ### An alternative to implicit dynamic method invocation is to use wildcard

                   ### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">

                   struts.enable.DynamicMethodInvocation = true

                  

         1.2 配置Action指明  method 属性

                   通过这种方式可以把一个Action类映射为多个逻辑上的Action

         1.3 使用通配符

                   一个Action的配置映射多个Action

                   约定优先

                   <!-- 当执行一个Action的时候,先找严格匹配的,如果没有严格匹配的,按照从上到下的顺序进行匹配,找到第一可以成功匹配的就执行 -->

                  

2. 如何获得用户提交的表单信息   

         2.1 属性驱动

         2.2 Model Driven (ActionForm)

                   2.2.1 首先提供model,功能上类似于struts1ActionForm, 最好实现Serializable 接口

                   2.2.2 Action implements ModelDriven<model>

                   2.2.3 Action中自己实例化model的实例: private UserInfo userInfo = new UserInfo();

                   2.2.4 重写getModel方法,返回model的实例: public UserInfo getModel() {return userInfo;}

                   2.2.5  <s:property value="model.info"/>

         2.3 属性驱动,属性是Javabean      

                   private UserInfo userInfo = null;

                   public UserInfo getUserInfo() {

                            return userInfo;

                   }

 

                   public void setUserInfo(UserInfo userInfo) {

                            this.userInfo = userInfo;

                   }

                    <s:textfield name="userInfo.username">  属性名。属性

                    

3. 访问servlet API

         3.1   Map 类型 servlet 对象

                   3.1.1         依赖注入

                                     import org.apache.struts2.interceptor.RequestAware;

                                     import org.apache.struts2.interceptor.ApplicationAware;

                                     import org.apache.struts2.interceptor.SessionAware;

                   3.1.2        通过特定的API

                                     Map<String, Object> session = ActionContext.getContext().getSession();

                                     Map<String, Object> application = ActionContext.getContext()

                                     .getApplication();

                                     Map<String, Object> request = (Map<String, Object>) ActionContext

                                               .getContext().get("request");

                  

         3.2   拿真实的servlet API对象  

                   3.2.1        依赖注入

                                     import org.apache.struts2.interceptor.ServletRequestAware;

                   3.2.2      通过特定的API

                                     ServletActionContext.getRequest()

                                     ServletActionContext.getRequest().getSession()

                                    

4. OGNL is the Object Graph Navigation Language

         struts2的标签默认使用的表达式 OGNL

5. 简单的数据校验(ognl对象导航)

 

6. ResultType

         <action name="login" class="com.pk.action.LoginAction">

                            <result type="redirectAction">checkEmail?id=${id}</result>

         </action>

         此次${id} 意味着从值栈中取id的值

        

         Action的链式调用中,ActionContext.getContext().getValueStack().findString("id")

         取得上个Action 值栈里面的信息

        

         往其它包里面掉转的方式

         <result type="redirectAction">

                <param name="actionName">HelloWorld</param>

                <param name="namespace">/example</param>

         </result>

        

7. result的范围

         result定义在Action的内部,称局部result,供本Action使用

         全局result ,供本包所有的Action共享

         <global-results>

                            <result>/welcome.jsp</result>

         </global-results>         

         就近原则,局部优先

        

8. struts2 spring的整合

         8.1   复制struts2jar

         8.2   配置struts2核心控制器

         8.3   提供struts2的配置文件

        

         8.4   复制springjar

         8.5   提供spring的配置文件

         8.6   提供log4j的配置文件

         8.7   启动spring IOC容器

        

         8.8   整合: Action访问到IOC容器管理的业务层的Bean

                   添加一个额外的jar:struts2-spring-plugin-2.1.8.1.jar

                  

                   8.8.1         依赖于自动装配

                            在默认情况下,按名字进行自动装配,可以通过下面的配置改为按类型自动装配

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

                           

                   8.8.2        Action交给spring进行管理

                            多配置一个bean

                            <bean id="abc" class="com.pk.action.RegistAction" scope="prototype">

                                     <property name="registService" ref="registService"></property>

                            </bean>

                            beanid??

                            只要保证<action name="regist" class="跟上面的id一致"></action>

                           

你可能感兴趣的:(apache,spring,bean,struts,配置管理)