歡迎您光臨本站 註冊首頁

實現一個簡單的struts和spring框架

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

apache終於發布了struts2的正式版:struts2.0.6GA,所以我也該從webwork遷移至struts2.struts2基本上就是webwork的翻版,所以遷移過程倒是很簡單,只需要修改下配置文件和一些包名就可以了.如果在Eclipse、Netbeans這些集成開發工具的幫助下,記不清包名也很容易找到想要的類的,呵呵.

在Eclipse下建立一個Dynamic Web Application.

從struts2.0.6的lib目錄中複製下面的庫文件到WEB-INF/lib目錄下:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.9.jar
struts-api-2.0.6.jar
struts-core-2.0.6.jar
struts-spring-plugin-2.0.6.jar
xwork-2.0.0.jar

從spring中lib目錄中複製下面的庫文件到WEB-INF/lib目錄下:
spring.jar

修改web.xml,增加一個struts的分派器filter,映射所有的url-pattern,再增加一個spring的ContextLoaderListenerJianTingQi.修改後的內容如下:
xml 代碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts2tutorial</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>*.action</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

寫一個簡單的Action,HelloWorld:
java 代碼

package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {

public static final String MESSAGE = "Struts is up and running ...";

public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}

private String message;

public void setMessage(String message){
this.message = message;
}


public String getMessage() {
return message;
}
}


在源文件路徑下(項目的src目錄)增加struts.xml配置action.這個文件是集成spring的關鍵所在,這裡面描述有如何將spring2集成到struts2的相關信息:
xml 代碼

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 下面這句表明對象是由spring負責產生的.加上這句后,struts會產生讓spring負責
產生bean,如果spring不能產生bean,則由struts自己產生.也可以在struts.properties
文件內定義這個屬性.-->
<constant name="objectFactory" value="spring"></constant>
<package name="struts2tutoial" extends="struts-default" namespace="/">
<!-- 注意,現在action的class屬性不再是類的名字了,而是在spring中的bean的id
詳細信息請看下面的spring的bean配置文件applicationContext.xml -->
<action name="HelloWorld" class="helloWorld">
<result>/helloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>


在WEB-INF/目錄下增加spring的bean配置文件applicationContext.xml:
xml 代碼

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<bean id="helloWorld" class="tutorial.HelloWorld"></bean>

</beans>


配置很簡單,只有一個bean.

,在WebContent目錄下增加helloWorld.jsp:
xml 代碼

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
</body>
</html>


[火星人 ] 實現一個簡單的struts和spring框架已經有799次圍觀

http://coctec.com/docs/java/show-post-61598.html