Spring与Struts2整合
Spring与Struts2整合
Spring是一个非常优秀的框架可以完美的融合其他框架,框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为开发 J2EE 应用程序提供集成的框架, Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
首先这次是Spring与Struts2的整合:
1.首先建立一个web项目,在lib下面添加struts2和Spring的jar文件。
2.在WEB-INF下面添加web.xml和applicationContext.xml
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring_Struts</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- listener是Servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作,如监听到在线的数量。当增加一个HttpSession时, 就激发sessionCreated()方法。监听器需要知道application.xml配置文件的位置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <bean id="loginAction" class="com.zdx.action.LoginAction"></bean> </beans>
3.在src下面添加struts.properties和struts.xml:
struts.properties:
struts.objectFactory=spring
表示struts的对象由Spring容器来管理
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default"> <action name="loginAction" class="loginAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
上面就是Spring和Struts2的整合,下面我们通过一个例子进行验证:
4.编写Action:
package com.zdx.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String name; private String pwd; @Override public String execute() throws Exception { // TODO Auto-generated method stub return super.execute(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
index.jsp:
<body> <s:form action="loginAction"> <s:textfield name="name" label="姓名" ></s:textfield> <s:password name="pwd" label="密码:"></s:password> <s:submit value="登陆"></s:submit> </s:form> </body>
success.jsp:
<body> welcome <s:property value="name" /> !!! </body>
以上就是struts2和Spring的整合
相关推荐
白净垃圾桶 2020-05-04
melonjj 2020-01-05
yaoyao0 2014-06-01
YangHuiLiang 2014-05-20
方志朋 2015-04-30
殷龙飞 2014-06-24
csdnstudylp 2019-08-23
hgzhang 2020-05-06
playis 2020-04-18
郭大路路 2020-04-16
lenchio 2020-04-08
yixiaoqi00 2020-04-08
mmywcoco 2019-12-28
wangruiling 2014-06-13
方志朋 2019-12-22
xcguoyu 2014-01-18
zmysna 2016-12-16
89264255 2011-04-23