溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring整合struts2過程詳解

發(fā)布時間:2020-08-29 20:31:13 來源:腳本之家 閱讀:176 作者:西西嘛呦 欄目:編程語言

這篇文章主要介紹了spring整合struts2過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

首先將以下jar包加入到lib文件夾中:

spring整合struts2過程詳解

spring整合struts2過程詳解

基礎目錄:

spring整合struts2過程詳解

Person.java

package com.gong.spring.struts2.beans;

public class Person {
  
  private String username;
  
  public void setUsername(String username) {
    this.username = username;
  }
  
  public void hello(){
    System.out.println("My name is " + username);
  }
  
}

PersonService.java

package com.gong.spring.struts2.services;

public class PersonService {
  
  public void save(){
    System.out.println("PersonService's save....");
  }
  
}

PersonAction.java

package com.gong.spring.struts2.actions;

import com.gong.spring.struts2.services.PersonService;

public class PersonAction {
  
  private PersonService personService;
  
  public void setPersonService(PersonService personService) {
    this.personService = personService;
  }
  
  public String execute(){
    System.out.println("execute....");
    personService.save();
    return "success";
  }
  
}

基本流程如下:在PersonAction裝配PersonService,在execute方法中打印相關信息并調(diào)用personService的save方法,最后返回"success"。在PersonService中的save方法輸出一句話。

applicationContext.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="person" 
    class="com.gong.spring.struts2.beans.Person">
    <property name="username" value="spring"></property>  
  </bean>
  
  <bean id="personService"
    class="com.gong.spring.struts2.services.PersonService"></bean>
  
  <!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 時, 需要配置 scope 屬性, 其值必須為 prototype -->
  <bean id="personAction" 
    class="com.gong.spring.struts2.actions.PersonAction"
    scope="prototype">
    <property name="personService" ref="personService"></property>  
  </bean>
  
</beans>

在applicationContext中配置相關bean。

stuts.xml

<?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>

  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="default" namespace="/" extends="struts-default">
    
    <!-- 
      Spring 整合 Struts2 時, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中該 bean 的 id
    -->
    <action name="person-save" class="personAction">
      <result>/success.jsp</result>
    </action>
    
  </package>

</struts>

在struts.xml中配置action時,class需要使用applicationContext.xml中bean的id。結(jié)果返回給success.jsp。

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" 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">

  <!-- 配置 Spring 配置文件的名稱和位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 啟動 IOC 容器的 ServletContextListener -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置 Struts2 的 Filter -->
  <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>

在web.xml中需要兩個部分:一個是讓springIOC容器在web應用服務加載時就進行創(chuàng)建。另一個就是配置struts2的過濾器。

index.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>Insert title here</title>
</head>
<body>
  
  <a href="person-save" rel="external nofollow" >Person Save</a>
  
</body>
</html>

sucess.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>Insert title here</title>
</head>
<body>
  
  <h5>Success Page</h5>
  
</body>
</html>

test.jsp

<%@page import="com.gong.spring.struts2.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ 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>Insert title here</title>
</head>
<body>
  
  <% 
    //1. 從 appication 域?qū)ο笾械玫?IOC 容器的實例
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
    
    //2. 從 IOC 容器中得到 bean
    Person person = ctx.getBean(Person.class);
    
    //3. 使用 bean
    person.hello();
  %>
  
</body>
</html>

啟動tomacat服務器之后:

spring整合struts2過程詳解

點擊Person Save:

spring整合struts2過程詳解

會跳轉(zhuǎn)到succes.jsp,并在控制臺輸出相應的語句。

說明spring整合struts2基本是成功的了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI