这两天一直致力于struts+spring 的学习,本想在在网上找一些例子来学习,虽说链接不少,但是真正能给予帮助的很少,要么就是断章取义,要么就是解释不清,最后只好自己琢磨。没有想到,最后还真的成功了。现在精髓部分贴出供大家学习。
1.struts-config.xml配置文件是关键:
<struts-config>
<form-beans>
<form-bean name="username" type="com.jackbooth.action.Username" /> //表单
</form-beans>
<action-mappings>
<action
attribute="username"
name="username" //指明页面加载的表单
path="/loginCheck" //登陆页面的action
type="org.springframework.web.struts.DelegatingActionProxy"> //指明负责action的处理类(spring处理)
<forward name="login" path="/WEB-INF/jsp/login.jsp" />
<forward name="success" path="/WEB-INF/jsp/success.jsp" />
</action>
//这是一个登陆的路径
<action
path="/login"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/login.jsp" />
</action-mappings>
<message-resources parameter="com.jackbooth.action.ApplicationResources" />
<!-- 把spring注册到struts里来 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
2.applicationContext.xml
由于在 struts-config.xml中指定了action处理类是DelegatingActionProxy,那么在applicationContext中就要根据相应的path来添加依赖注入。
3.看看login.jsp
<form name="Username" action="/loginCheck.do" method="post">指明了,submit的处理action =loginCheck.而这个路径和struts-config.xml中指定path一致。
如此,struts和spring 完美结合