怎么解决在action中每次都要从spring管理的配置中区获取对象有关问题
如何解决在action中每次都要从spring管理的配置中区获取对象问题
1. 在action中每次都要从spring管理的配置中区获取对象 , 所以考虑把此配置放到web的application中
容器启动就加载,
方法一 .配置如下:
<!-- 配置如下 名字有约定,由spring内部提供 , 也可以自己写一个监听器 -->
在web.xml中配置如下:
3. action中的用法如下:
方法二:struct中提供了plugin机制 (用的较多)
配置如下:
在struts-config.xml 配置
<!-- 容器一启动后就加载plug-in 以下配置的就是把xml文件读入web的application中 -->
在action中要取dao 只需将dao注入即可,再也不用显示的用ctx.getBean(“useDao”)了
如:
方法三:和方法2差不多
在web.xml中配置
在stucts.config.xml中配置
在spring的application-action.xml中配置
1. 在action中每次都要从spring管理的配置中区获取对象 , 所以考虑把此配置放到web的application中
容器启动就加载,
方法一 .配置如下:
<!-- 配置如下 名字有约定,由spring内部提供 , 也可以自己写一个监听器 -->
在web.xml中配置如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3. action中的用法如下:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()) ; UserDao userDao = (UserDao)ctx.getBean("userDao") ;
方法二:struct中提供了plugin机制 (用的较多)
配置如下:
在struts-config.xml 配置
<!-- 容器一启动后就加载plug-in 以下配置的就是把xml文件读入web的application中 -->
<!-- action的创建工作交由spring来管理, action过来,去actionMapping中查找 , 找到的是一个spring的 代理类 , 读取配置交由DelegatingActionProxy来读取 , 所以程序员根本无需显示去读取配置,因为已经 交由spring注册进去了,自动管理 --> <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="userActionForm" > <forward name="success" path="/success.jsp"></forward> <forward name="fail" path="/fail.jsp"></forward> </action> </action-mappings> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext-*.xml" /> </plug-in> 在spring的application-action.xml中配置 <bean name="/login" class="com.cs.action.LoginAction"> <property name="userDao" ref="userDao" /> </bean>
在action中要取dao 只需将dao注入即可,再也不用显示的用ctx.getBean(“useDao”)了
如:
public class LoginAction extends Action { private UserDao userDao ; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserActionForm userActionForm = (UserActionForm)form ; User user = userDao.login(userActionForm.getUsername(), userActionForm.getPassword()) ; if (user != null) { return mapping.findForward("success") ; }else { return mapping.findForward("fail") ; } }
方法三:和方法2差不多
在web.xml中配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
在stucts.config.xml中配置
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="userActionForm" > <forward name="success" path="/success.jsp"></forward> <forward name="fail" path="/fail.jsp"></forward> </action>
在spring的application-action.xml中配置
<!-- 在此name对应struts配置中的path --> <bean name="/login" class="com.cs.action.LoginAction" > <property name="userDao" ref="userDao" /> </bean>