这是另外一种SSH集成的方式,相比上一种来说,更简单,也更灵活
首先建立一个web项目,和上文一样。
下面贴代码:
BaseAction:
public class BaseAction extends DispatchActionSupport {
public Object getBean(String name)
{
WebApplicationContext ctx = this.getWebApplicationContext();
return ctx.getBean(name);
}
}
注意:这里的Action继承的是Spring的DispatchActionSupport
Action中:
public class UserAction extends BaseAction {
public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserLoginForm fm = (UserLoginForm)form;
IUserService userService = (IUserService)this.getBean("userService");
……
……
}
}
web.xml,基本上和上次的配置是一样的
<!-- 让容器自动加载Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
struts-config.xml,注意,这次type指向的就是真正的Action类了,而不是Spring代理
<form-beans>
<form-bean name="UserLoginForm" type="com.king.struts.form.UserLoginForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/app/userOperation" type="com.king.struts.action.UserAction"
scope="session" name="UserLoginForm" parameter="o">
<forward name="loginPage" path="/user/login.jsp"></forward>
<forward name="firstPage" path="/user/UserInfo.jsp"></forward>
</action>
</action-mappings>
applicationContext.xml,配置文件中没有配置关于Action的东西
<!-- 下面就是具体的业务配置 Action Service DAO-->
<!-- User -->
<bean id="userDAO" class="com.king.dao.impl.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.king.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
基本上就是这个样子,功能可以基本实现
关于Spring的事务配置,不是很清楚,所以一直在回避。等看明白了,再做个笔记
使用这种方法的时候,配置较为简单,我比较喜欢