spring3整合struts2

    xiaoxiao2021-03-25  104

    action名字中可以出现“/”,就要在struts2配置文件中配置struts.enable.SlashesInActionNames=true <constrant name="struts.enable.SlashesInActionNames" value="true" />

    除了struts2本身的配置文件外,还要做下列的事

    在struts.xml文件中配置 <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

    使得struts2能在spring的配置文件中读取action。action的名字需要与在spring配置文件中的bean名字相同

    Struts2的Action在Spring中配置,<bean >中的属性是prototype,因为Struts2的Action是有状态的 <bean name="" class=""> <!-- 填action的名字和其全限定名 --> <property name="" ref=""/>      <!-- 填action类中的属性,实现注入-->

     </bean>

    这样,struts2的配置文件中action标签的class属性只需填写类名即可,不用写类的全限定命名 为了让服务器认识spring配置文件 在web.xml中配置 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring配置.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <global-results>里定义通用的错误页面,成功页面 Action类可以实现RequestAware接口来让框架运行时候接受request,用以存放数据,这样就不用每次都去创建一个request来存放数据

    Action类可以使用如下方法,方便数据的存取,简化代码

    模型驱动:           模型驱动是使用javaBean对象来封装请求参数,在整个MVC流程中可以直接使用这个对象。在发送请求后Struts将各个请求参数(javab的属性)封装到一个JavaBean对象中,Action接收这个实例对象并用该对象进行相关处理返回处理结果。           模型驱动的Action中定义javaBean并实例化,并且需要提供该JavaBean的getter和setter方法。Action类需要实现ModelDriven<T>接口。
    转载请注明原文地址: https://ju.6miu.com/read-13001.html

    最新回复(0)