spring整合struts2過程詳解
這篇文章主要介紹了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基本是成功的了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring與Struts整合之讓Spring管理控制器操作示例
- struts2+spring+ibatis框架整合實(shí)現(xiàn)增刪改查
- WebSocket整合SSM(Spring,Struts2,Maven)的實(shí)現(xiàn)示例
- Spring+Hibernate+Struts(SSH)框架整合實(shí)戰(zhàn)
- struts+spring+hibernate三個(gè)框架的整合
- Spring整合Struts2的兩種方法小結(jié)
- 詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
- struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成簡單demo
- SSH框架網(wǎng)上商城項(xiàng)目第1戰(zhàn)之整合Struts2、Hibernate4.3和Spring4.2
- struts2 spring整合fieldError問題
- Spring與Struts整合之使用自動(dòng)裝配操作示例
相關(guān)文章
java基礎(chǔ)之初始化ArrayList時(shí)直接賦值的4種方式總結(jié)
ArrayList是Java中的一個(gè)類,它是Java集合框架中的一部分,用于實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于java基礎(chǔ)之初始化ArrayList時(shí)直接賦值的4種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
MyBatis深入解讀動(dòng)態(tài)SQL的實(shí)現(xiàn)
動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一。如果你使用過 JDBC 或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接 SQL 語句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(dòng)態(tài) SQL,可以徹底擺脫這種痛苦2022-04-04
Java從源碼看異步任務(wù)計(jì)算FutureTask
這篇文章主要介紹了Java從源碼看異步任務(wù)計(jì)算FutureTask,F(xiàn)utureTask就能夠很好的幫助我們實(shí)現(xiàn)異步計(jì)算,并且可以實(shí)現(xiàn)同步獲取異步任務(wù)的計(jì)算結(jié)果,具體是怎樣實(shí)現(xiàn)的,下面我們就一起來學(xué)習(xí)下面文章的具體內(nèi)容吧2022-04-04
SpringCloud?eureka(server)微服務(wù)集群搭建過程
這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項(xiàng)目搭建的主要步驟和配置就是創(chuàng)建項(xiàng)目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
你應(yīng)該知道的這些Mybatis-Plus使用技巧(小結(jié))
這篇文章主要介紹了你應(yīng)該知道的這些Mybatis-Plus使用技巧(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Springboot2.X集成redis集群(Lettuce)連接的方法
這篇文章主要介紹了Springboot2.X集成redis集群(Lettuce)連接的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07

