Mind_programmonkey的博客

苔花如米小,也学牡丹开!!!

Struts2 篇--02 struts与spring的初步整合

1.建立maven工程,在pom.xml中导入依赖包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lcz</groupId>
  <artifactId>struts_spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-spring-plugin</artifactId>
  		<version>2.5.13</version>
  	</dependency>
  	<dependency>
	  <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-core</artifactId>
	  <version>2.5.13</version>
	</dependency>
  </dependencies>
</project>

2.web.xml中的配置

 <filter>
    <display-name>StrutsPrepareAndExecuteFilter</display-name>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> 
 
  <!--用来在Web容器启动期间初始化Spring容器  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:conf/spring_*.xml</param-value>
  </context-param>

3.在resources下的struts.xml文件的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="login" namespace="/login" extends="struts-default">
		<!-- 使用由spring管理的action组件webaction,在配置action期间,将class属性引用bean id即可 -->
		<action name="web" class="webAction">
			<result name="success">/WEB-INF/success.jsp</result>
		</action>
	</package>
</struts>

4.spring_web.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:beans="http://www.springframework.org/schema/beans" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	
	xmlns:util="http://www.springframework.org/schema/util"  
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		">
	<context:component-scan base-package="com.lcz.web"/>
</beans>

5.WebAction文件

package com.lcz.web;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
public class WebAction{
	private String message;
	public String getMessage() {
		return message;
	}
	public String execute() {
		System.out.println("Web action");
		message="spring";
		return "success";
	}
}

阅读更多

扫码向博主提问

去开通我的Chat快问

mind_programmonkey

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Java EE
  • Java SE
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mind_programmonkey/article/details/80338215
个人分类: javaweb的学习路径
上一篇Struts2 篇--01 struts2的初体验--以建立一个登陆界面为例
下一篇阻塞和死锁的区别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭