亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring整合struts2過程詳解

 更新時間:2020年01月11日 09:38:53   作者:西西嘛呦  
這篇文章主要介紹了spring整合struts2過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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

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

基礎(chǔ)目錄:

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方法中打印相關(guān)信息并調(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中配置相關(guān)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應(yīng)用服務(wù)加載時就進(jìn)行創(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>
  
  <h4>Success Page</h4>
  
</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服務(wù)器之后:

點擊Person Save:

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java基礎(chǔ)之初始化ArrayList時直接賦值的4種方式總結(jié)

    java基礎(chǔ)之初始化ArrayList時直接賦值的4種方式總結(jié)

    ArrayList是Java中的一個類,它是Java集合框架中的一部分,用于實現(xiàn)動態(tài)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于java基礎(chǔ)之初始化ArrayList時直接賦值的4種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • java實現(xiàn)選擇排序算法

    java實現(xiàn)選擇排序算法

    本篇文章介紹直接選擇排序算法的JAVA實現(xiàn)。直接選擇排序算法的基本思想是:n個記錄的文件的直接選擇排序可經(jīng)過n-1趟直接選擇排序得到有序結(jié)果
    2015-04-04
  • java用類加載器的5種方式讀取.properties文件

    java用類加載器的5種方式讀取.properties文件

    這篇文章主要介紹了java用類加載器的5種方式讀取.properties文件,詳細(xì)的介紹了這5種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • MyBatis深入解讀動態(tài)SQL的實現(xiàn)

    MyBatis深入解讀動態(tài)SQL的實現(xiàn)

    動態(tài) SQL 是 MyBatis 的強大特性之一。如果你使用過 JDBC 或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接 SQL 語句有多痛苦,例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最后一個列名的逗號。利用動態(tài) SQL,可以徹底擺脫這種痛苦
    2022-04-04
  • 簡述Mybatis增刪改查實例代碼

    簡述Mybatis增刪改查實例代碼

    本文給大家分享編寫一個簡單的mybatis進(jìn)行插入數(shù)據(jù)的實例代碼,非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • Java從源碼看異步任務(wù)計算FutureTask

    Java從源碼看異步任務(wù)計算FutureTask

    這篇文章主要介紹了Java從源碼看異步任務(wù)計算FutureTask,F(xiàn)utureTask就能夠很好的幫助我們實現(xiàn)異步計算,并且可以實現(xiàn)同步獲取異步任務(wù)的計算結(jié)果,具體是怎樣實現(xiàn)的,下面我們就一起來學(xué)習(xí)下面文章的具體內(nèi)容吧
    2022-04-04
  • SpringCloud?eureka(server)微服務(wù)集群搭建過程

    SpringCloud?eureka(server)微服務(wù)集群搭建過程

    這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項目搭建的主要步驟和配置就是創(chuàng)建項目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • 你應(yīng)該知道的這些Mybatis-Plus使用技巧(小結(jié))

    你應(yīng)該知道的這些Mybatis-Plus使用技巧(小結(jié))

    這篇文章主要介紹了你應(yīng)該知道的這些Mybatis-Plus使用技巧(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Springboot2.X集成redis集群(Lettuce)連接的方法

    Springboot2.X集成redis集群(Lettuce)連接的方法

    這篇文章主要介紹了Springboot2.X集成redis集群(Lettuce)連接的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java?web實現(xiàn)購物車案例

    Java?web實現(xiàn)購物車案例

    這篇文章主要為大家詳細(xì)介紹了Java?web實現(xiàn)購物車案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論