Spring整合Struts2

Spring自己提供了一套web框架,同时也支持第三方web框架,下面就简单记录下spring整合struts2

因为是WEB工程,所以先在eclipse里面创建web项目,准备使用的jar包,struts2和spring官网都可以下载。

1. 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" 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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   	 </listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/config/springConfig.xml</param-value>
	</context-param>
	
	<!-- 配置struts -->
	<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>
	

</web-app>

 struts2是通过过滤器来拦截我们的请求,所以使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter来进行拦截,这里拦截所有的请求

使用org.springframework.web.context.ContextLoaderListener来启动spring容器

2.创建HelloAction

package com.jacksoft.spring.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String str;

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("sss:" + getStr());
		return SUCCESS;
	}

}

 这个action比较简单,只有一个属性str,加上getter和setter方法,等下用spring来注入数据

3.创建hello.jsp

在WEB-INF/jsp下面创建hello.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>Spring-Struts2</title>
</head>
<body>
Hello:${str}
</body>
</html>

 简单使用表达式来将刚刚action里面的str显示出来

4.springConfig.xml

将HelloAction交给spring来管理,每次都要创建实例,所以scope=prototype,为str注入数据

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
						http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 每次都创建实例 -->
	<bean id="helloAction" class="com.jacksoft.spring.struts.action.HelloAction" scope="prototype">
		<property name="str" value="Jack"/>
	</bean>
</beans>

 5.struts.xml

struts在创建action的时候,也是一个和spring一样的容器,而现在我们要使用spring来管理,所以需要我们指定struts.objectFactory为spring,同时helloAction已经交给spring管理了,所以我们配置的action对应的class应该为spring里面对应的bean,不然spring没法管理,仍然还是有struts自己创建action

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 
	<constant name="struts.objectFactory"
		value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
 -->		
	<constant name="struts.objectFactory"
		value="spring" />	
		
	<constant name="struts.devMode" value="true" />
	
	<package name="default" extends="struts-default">
		<action name="hello" class="helloAction">
			<result name="success">/WEB-INF/jsp/hello.jsp</result>
		</action>
	</package>
</struts>

 

这里struts.objectFactory的值可以简单的写成spring,也可以完全指定:org.apache.struts2.spring.StrutsSpringObjectFactory

6.Web Server

这里使用Jetty来作为web服务器,所以需要编写启动程序类

package com.jacksoft.spring.struts.webserver;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

/**
 *  创建Jetty容器
 * @author Jack
 *
 */
public class WebServer {

	public static void main(String[] args) {
		try{
			Server server = new Server(8080);
			WebAppContext context = new WebAppContext();
			context.setContextPath("/springTest");
			context.setResourceBase("./WebContent");
			server.setHandler(context);
			server.start();
			server.join();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

 指定访问路径和加载路径

 接下来就可以访问了,http://localhost:8080/springTest/hello

 对应工程目录结构:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值