zoukankan      html  css  js  c++  java
  • Spring整合Struts2

    Spring整合struts2的步骤: 
       1.导包 
       2.配置web.xml 
       3.配置applicationContext.xml 
       4.创建并声明DAO 
       5.创建并声明Action 
       6.配置Action 
       7.创建JSP
    案例:使用Spring整合Struts2,并实现Struts2的HelloWorld案例
    步骤:
        实现此案例按如下步骤进行:
    
    步骤一:导包
    所需环境:struts2.5+spring4.0+tomcat7.0
    所需如下:

         https://files.cnblogs.com/files/demon09/struts.rar
         https://files.cnblogs.com/files/demon09/spring4.0.rar


    步骤二:配置web.xml 在web.xml中配置一个listener,用于tomcat启动时自动加载Spring,在配置出Struts2的前端控制器, 代码如下:

    <?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_2_5.xsd"
    	version="2.5">
    	<display-name>SpringStruts2</display-name>
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    		<welcome-file>index.htm</welcome-file>
    		<welcome-file>index.jsp</welcome-file>
    		<welcome-file>default.html</welcome-file>
    		<welcome-file>default.htm</welcome-file>
    		<welcome-file>default.jsp</welcome-file>
    	</welcome-file-list>
    	<!-- 配置listener,在容器启动的时候自动加载Spring -->
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext.xml</param-value>
    	</context-param>
    	<!-- 配置Struts2前端控制器 -->
    	<filter>
    		<filter-name>Struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>Struts</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
    </web-app>
    步骤三:配置applicationContext.xml
        引入Spring配置文件,放在src根路径下,在该文件下开启组件扫描,代码如下所示:
    <?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:jdbc="http://www.springframework.org/schema/jdbc"  
    	xmlns:jee="http://www.springframework.org/schema/jee" 
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    		
    	<!-- 开启注解扫描  -->
    	<context:component-scan base-package="com.whread" />
    	
    </beans>
    步骤四:创建并声明DAO
        创建包com.whread.dao,并在包下创建一个类HelloDao,在这个类中写一个say方法,其返回值为字符串,返回一句话即可, 另外要使用Spring注解声明这个DAO组件,将其纳入到Spring容器中,代码如下所示:
    package com.whread.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class HelloDao {
    	
    	public String say(){
    		return "Hello,Spring整合Struts2";
    	}
    }  
    步骤五:创建并声明Action
        创建包com.whread.action,并在包下创建HelloAction,在这个Action业务的方法中,调用HelloDao返回一句话,并通过输出属性输出给页面.需要使用Spring注解声明这个Action组件,将其纳入到Spring容器中,另外也需要通过注解将DAO注入进来.代码示例如下:
    package com.whread.action;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    
    import com.tarena.dao.HelloDao;
    
    @Controller
    public class HelloAction {
    
    	@Resource
    	private HelloDao dao;
    	
    	private String msg;
    	
    	public String execute(){
    		//通过dao获取输出消息
    		msg = dao.say();
    		System.out.println("msg="+msg);
    		return "success";
    	}
    
    	public String getMsg() {
    		return msg;
    	}
    
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    	
    	
    }
    步骤六:配置Action
        在struts.xml中配置Action(struts.xml放置于src根目录下即可),由于是用Spring管理Action,在action元素中通过class属性指定Action类型的时候,需要指定的是组件的ID,代码示例如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    <!-- package配置说明:
    		name属性:名字,值随意
    		namespace属性:命名空间,用于对应请求的URL中的path,即/main,注意,必须使用/作为前缀
    		extends属性:固定取值为struts-default -->
    	<package name="demo" namespace="/demo" extends="struts-default">
    		<!-- http://localhost:8080/SpringStruts2/demo/hello.action -->
    <!-- action配置说明:
    		name属性:名字,值为请求的资源名称,不包含 .action部分
    		class属性(可写包.类名):接收并处理请求的java类的包名与类名
    		method属性(可选):接收并处理请求的的java类的方法名  -->
    		<action name="hello" class="helloAction">
    			<result name="success">
    				/hello.jsp
    			</result>
    		</action>
    	</package>
    </struts>
    步骤七:创建JSP页面
        在WEB-INF下创建hello.jsp,并在这个页面上输出HelloAction中的输出属性,代码示例如下:
    <%@page pageEncoding="utf-8" isELIgnored="false"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <html>
    <head>
    </head>
    <body>
    <h1>
    	<s:property value="msg" />
    </h1>
    </body>
    </html>
    步骤八:测试
        部署项目,并启动tomcat,访问http://localhost:8080/SpringStruts2/demo/hello.action,成功出现Hello,Spring整合Struts2即表示成功
  • 相关阅读:
    Power BI for Office 365(八)共享查询
    Power BI for Office 365(七) Power BI站点
    Power BI for Office 365(六)Power Map简介
    Power BI for Office 365(五)Power View第二部分
    Power BI for Office 365(四)Power View第一部分
    Power BI for Office 365(三)Power Pivot
    Power BI for Office 365(二)Power Query
    java 继承、重载、重写与多态
    Android 热修复方案Tinker(一) Application改造
    阿里最新热修复Sophix与QQ超级补丁和Tinker的实现与总结
  • 原文地址:https://www.cnblogs.com/demon09/p/9709732.html
Copyright © 2011-2022 走看看