我的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的事務配置,不是很清楚,所以一直在迴避。等看明白了,再做個筆記

使用這種方法的時候,配置較爲簡單,我比較喜歡
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章