最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring整合struts2過程詳解

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

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

首先將以下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 時(shí), 需要配置 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 時(shí), 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中該 bean 的 id
    -->
    <action name="person-save" class="personAction">
      <result>/success.jsp</result>
    </action>
    
  </package>

</struts>

在struts.xml中配置action時(shí),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>
  
  <!-- 啟動(dòng) 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中需要兩個(gè)部分:一個(gè)是讓springIOC容器在web應(yīng)用服務(wù)加載時(shí)就進(jìn)行創(chuàng)建。另一個(gè)就是配置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 容器的實(shí)例
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
    
    //2. 從 IOC 容器中得到 bean
    Person person = ctx.getBean(Person.class);
    
    //3. 使用 bean
    person.hello();
  %>
  
</body>
</html>

啟動(dòng)tomacat服務(wù)器之后:

點(diǎn)擊Person Save:

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

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

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

相關(guān)文章

  • 如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred in scheduled task問題

    如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred 

    這篇文章主要介紹了如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred in scheduled task問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java string類型轉(zhuǎn)換boolean類型的方法

    java string類型轉(zhuǎn)換boolean類型的方法

    下面小編就為大家?guī)硪黄猨ava string類型轉(zhuǎn)換boolean類型的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • javaSE基礎(chǔ)如何通俗的理解javaBean是什么

    javaSE基礎(chǔ)如何通俗的理解javaBean是什么

    所謂的Java Bean,就是一個(gè)java類,編譯后成為了一個(gè)后綴名是 .class的文件。這就是Java Bean,很多初學(xué)者,包括當(dāng)年的我自己,總是被這些專有名詞搞的暈頭轉(zhuǎn)向
    2021-10-10
  • Java中Volatile關(guān)鍵字詳解及代碼示例

    Java中Volatile關(guān)鍵字詳解及代碼示例

    這篇文章主要介紹了Java中Volatile關(guān)鍵字詳解及代碼示例,分為兩個(gè)部分,第一部分介紹了Volatile關(guān)鍵字的基本概念等內(nèi)容,第二部分分享了實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 詳解Java在redis中進(jìn)行對(duì)象的緩存

    詳解Java在redis中進(jìn)行對(duì)象的緩存

    這篇文章主要介紹了Java在redis中進(jìn)行對(duì)象的緩存,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 基于java查找最長(zhǎng)字符串代碼實(shí)例

    基于java查找最長(zhǎng)字符串代碼實(shí)例

    這篇文章主要介紹了基于java查找最長(zhǎng)字符串代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 淺談Spring的兩種配置容器

    淺談Spring的兩種配置容器

    這篇文章主要介紹了淺談Spring的兩種配置容器,介紹了其實(shí)現(xiàn)以及簡(jiǎn)單的實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Spring AOP如何整合redis(注解方式)實(shí)現(xiàn)緩存統(tǒng)一管理詳解

    Spring AOP如何整合redis(注解方式)實(shí)現(xiàn)緩存統(tǒng)一管理詳解

    這篇文章主要給大家介紹了關(guān)于Spring AOP如何整合redis(注解方式)實(shí)現(xiàn)緩存統(tǒng)一管理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • SpringBoot重啟后,第一次請(qǐng)求接口請(qǐng)求慢的問題及解決

    SpringBoot重啟后,第一次請(qǐng)求接口請(qǐng)求慢的問題及解決

    這篇文章主要介紹了SpringBoot重啟后,第一次請(qǐng)求接口請(qǐng)求慢的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot集成WebSocket遇到的問題及解決

    SpringBoot集成WebSocket遇到的問題及解決

    這篇文章主要介紹了SpringBoot集成WebSocket遇到的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2023-07-07

最新評(píng)論

白城市| 百色市| 南靖县| 咸阳市| 博客| 乡宁县| 宁阳县| 上饶县| 竹北市| 闵行区| 金乡县| 阳朔县| 田林县| 安丘市| 河北省| 天全县| 中方县| 阿荣旗| 南江县| 湘乡市| 收藏| 册亨县| 凌云县| 徐汇区| 托里县| 阳江市| 皋兰县| 垫江县| 苗栗市| 新绛县| 郁南县| 华亭县| 凤山县| 延长县| 荃湾区| 梁山县| 东源县| 云南省| 吉林省| 马鞍山市| 都兰县|