我的SSH集成(Spring 只集成Hibernate,Struts自己管理自己)

这是另外一种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的事务配置,不是很清楚,所以一直在回避。等看明白了,再做个笔记

使用这种方法的时候,配置较为简单,我比较喜欢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值