1.构建Struts环境
配置xml文件
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
配置Struts2的配置文件
<action name="checkLogin" class="com.zion.ssh.demo.CheckLoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
建立Login.jsp文件
<form action="checkLogin"> UserName:<input type="text" name="username"/><br/> Password:<input type="password" name="password"/><br/> <input type="submit" value="submit"> <input type="reset" value="reset"> </form>
测试,访问成功
2.搭建Spring环境,与Struts2整合
修改xml文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
配置applicationContext.xml文件,配置Action的信息
<bean name="checkLogin" class="com.zion.ssh.demo.CheckLoginAction"/>
修改Struts.xml文件的class信息
<action name="checkLogin" class="checkLogin">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
测试,可正常访问
源码:https://github.com/zionlew/zion