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的整合