`
lushuaiyin
  • 浏览: 676228 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts集成spring官方例子

 
阅读更多
首先到web.xml文件中添加的org.springframework.web.context.ContextLoaderListener。


<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts2Example14</display-name>
  	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

applicationContext.xml文件默认情况下,将被用于做Spring bean配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="helloWorldClass" class="com.vaannila.HelloWorld" >
    	<property name="message" value="Hello World!" />
    </bean>
</beans>

正如你可以看到,我们已经注册的 HelloWorld类,并注入了“Hello World !“
消息消息属性使用setter注入法。


所有的Struts 2 action配置在struts.xml文件。 

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="helloWorld" class="helloWorldClass">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>


这里唯一的改变,而不是指com.vaannila.HelloWorld类,
我们直接与使用Spring bean配置文件中给定的bean的名字。


下面是HelloWorld类。 在execute()方法,我们就返回“ 成功 ” 的消息属性设置使用setter注入。 

package com.vaannila;

public class HelloWorld {

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String execute() {
        return "SUCCESS";
    }
}

在index.jsp页面中, 我们提出的HelloWorld行动的要求。
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=helloWorld.action">

调用execute()方法后,用户将被Success.jsp页面。 在此页中,我们dispaly的消息值。

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
${message} 
</body>
</html>


您需要在 WEB-INF/lib目录下面的jar文件。

commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.1
freemarker-2.3.13
junit-3.8.1
ognl-2.6.11
struts2-convention-plugin-2.1.6
struts2-core-2.1.6
xwork-2.1.2
 
struts2-spring-plugin-2.1.6

antlr-runtime-3.0
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
org.springframework.web-3.0.0.M3
org.springframework.web.servlet-3.0.0.M3




工程结构:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics