struts2和spring整合,基於註解的方法
直接碼程式碼
專案目錄
1.匯入struts和spring各自需要的jar包, 再匯入兩者整合需要的jar包
2.UserDao.java
package com.ssh.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void add() {
System.out.println("userDao ......");
}
}
UserServiece.java
package com.ssh.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.ssh.dao.UserDao; //@Service(value="userService")--value="userService"可不要,因為UserServiece的預設bean的name為小寫字母開頭的類名 @Service public class UserService { @Resource private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("userService ......"); userDao.add(); } }
UserAction.java
package com.ssh.action; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.ssh.service.UserService; @Controller @Scope("prototype") public class UserAction extends ActionSupport { @Autowired private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { System.out.println("userAction ......."); // 呼叫userService中方法發 userService.add(); return NONE; // 表示返回到任何頁面中去 } }
2.三個配置檔案
struts對應的struts.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> <!-- 在struts.xml中指定objectFactory物件工廠為spring,即action的建立交由spring進行 --> <constant name="struts.objectFactory" value="spring"/> <package name="demo1" extends="struts-default" namespace="/"> <!-- class屬性是從Spring IoC中獲取的 --> <action name="userAction" class="userAction"> </action> </package> </struts>
spring對應的applicationContext.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入的Spring的約束,已經很全了 -->
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 開啟註解掃描 -->
<context:component-scan base-package="com.ssh">
<!-- 不掃描controller包下的UserController類,既去除註解 -->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
</context:component-scan>
<!-- userDao -->
<!-- <bean id="userDao" class="com.ssh.dao.UserDao"></bean> -->
<!-- userService -->
<!-- <bean id="userService" class="com.ssh.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean> -->
<!-- 配置action的物件 -->
<!-- 注意action是多例項的,因此我們這裡把scope配置為prototype的 -->
<!-- <bean id="userAction" class="com.ssh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> -->
</beans>
web.xml<web-app>
<!-- 1.配置struts2的過濾器 -->
<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>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring配置檔案對應的引數,如果applicationContext.xml放在/WEB-INF/目錄下,可以忽略;若不是,則需要配置 -->
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param> -->
<!-- 2.配置Spring的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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="userAction">點選後會,傳送action為userAction的請求,由攔截器攔截,跳轉到指定頁面</a>
</body>
</html>
執行index.jsp進行測試
相關推薦
struts2和spring整合,基於註解的方法
直接碼程式碼專案目錄1.匯入struts和spring各自需要的jar包, 再匯入兩者整合需要的jar包2.UserDao.javapackage com.ssh.dao; import org.springframework.stereotype.Repository;
MyBatis和Spring整合,流程淺析 - 20181208
【面試題】我們在開發mybaits時候,有幾種寫sql語句的方式? 一種是註解、一種是xml檔案、還有一種是@Provider 註解形式:簡單但是不是很靈活,對於動態條件查詢是無法實現的,這時,我們可以使用xml的方式。 xml形式:有xml檔案,且約束嚴格。 @Provider:
SpringBoot和Mybatis整合一(基於註解)
專案結構: pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww
Struts2和Spring整合時使用Struts-plugin的注意點
Struts2和Spring整合有兩種方法可以使用: 一種是把Struts2的action交給Spring管理,action的配置中不需要指具體的類名,只需要指定配置在Spring中的那個actio
struts2和spring整合時需要注意的地方
1、首先要記得匯入依賴包:除了一些基本的包之外,還要記得匯入這兩個包——commons-logging和struts2-spring-plugin commons-loggin.jar是Spring的
shiro和Spring整合使用註解時沒有執行realm的doGetAuthorizationInfo回撥方法的解決
在使用Shiro框架進行專案整合時,使用註解在使用Shiro框架進行專案整合時,使用註解在使用Shiro框架進行專案整合時,使用註解@RequiresPermissions為方法提供是需要的許可權,但是根本沒有進行驗證,後面發現在自己的Realm中只執行了doGetAuth
Struts2、hibernate和spring下載,整合所需jar包
ssh三個框架各自所有版本下載地址如下 Struts framework 下載地址: spring framework 下載地址: ------------------------------------- struts2 commons-logging
spring自帶的定時任務功能,基於註解和xml配置
1、spring的配置檔案 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml
SpringBoot整合MyBatis(iBatis),基於註解和XML兩種方式
工具 IDEA Maven 專案建立 1. 通過IDEA建立SpringBoot專案 2. 結構目錄和JAVA版本選擇 3. 新增MySQL和MyBatis支援 4. 新增Lombok外掛,簡化GET、SET方法 5. WEB支援和啟動類 6. 專
Spring的AspectJ的AOP,基於註解(9.1)重點掌握
Spring註釋方式自動代理什麽是AspectJ AspectJ是一個面向切面的框架,它擴展了Java語言。AspectJ定義了AOP語法所以它有一個專門的編譯器用來生成遵守Java字節編碼規範的Class文件。AspectJ是一個基於Java語言的AOP框架Spring2.0以後新增了對AspectJ切點表
spring boot整合mybatis基於註解開發以及動態sql的使用
讓我們回憶一下上篇部落格中mybatis是怎樣發揮它的作用的,主要是三類檔案,第一mapper介面,第二xml檔案,第三全域性配置檔案(application.properties),而今天我們就是來簡化mybatis的工作的——利用註解替代xml配置檔案。 先貼出mapper介面程式碼 @
Spring-boot 整合RabbitMQ ------基於註解的開發
spring: rabbitmq: host: 192.168.3.193 username: guest password: guest package com.shi.amqp; import org.springframework.amqp.rab
eclipse下maven管理Spring專案構:SpringAOP,基於XMl,基於註解宣告事務,及事務的傳播行為
一:在applicationContext.xml中加入 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
JAVA 資料庫讀取blob(圖片)合成多張圖 基於Struts2和Spring
今天工作要求把存在資料庫的圖片(blob)讀取出來,之前沒有做過所以找了不少資源,在這裡記錄一下。因為用的是jdbcTemplate,在這裡一起貼出來,以防忘了。因為資料庫查出來的圖片是多張圖,在這裡返回List,到前臺再轉成byte[]。有些方法是在查詢時直接轉成byte[
spring boot +security+oauth認證伺服器和資源伺服器(基於註解實現)
部分程式碼是搬別人寫好、自己做了調整 一、認證伺服器配置 1.新建maven專案pom.xml <dependencies> <dependency> <groupId>
Redis 和 spring整合時,Jar包要匹配
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-red
IDEA下使用maven整合Struts2和Spring
初學java,在用struts做了幾個專案之後,覺得手動整合業務元件有些麻煩了,是時候上spring了。先把struts和spring整合下,體驗一把spring的IoC看看再說。 從網上找了不少資料,總感覺寫的不是很全面,對於我這個新手來說不是很友好。好在搗鼓了幾個小時之後,順利跑起來了
spring 和 redis整合,並且使用redis做session快取伺服器
所需jar commons-pool2-2.0.jar jedis-2.9.0.jar spring-data-redis-1.6.2.RELEASE.jar spring-session-1.2.1.RELEASE.jar 一 . xml配
Struts2和Spring進行整合
Struts2整合Spring,整合方式一: 動作類還是Struts2負責管理,只是向Spring容器要service的例項 0、拷貝jar包 1、搭建Spring的web環境 WEB-INF下web.xml檔案 <!-- we
SSH開發環境的配置:web.xml、Spring整合hibernate和Spring整合Struts2
一、struts + hibernate + Spring整合開發包的匯入:從myeclipse整合開發工具中匯入或者從相應網站下載然後放在工程專案WEB-INF包下的lib包內即可。 二、本文主要說明開發包匯入後對配置檔案的配置。 三、相應配置檔案的配置:web.xml、