Spring3,struts2入門,註解方法

Maven項目下spring3 ,struts2整合入門。 java

 spring 3 的配置文件,名稱spring.xml 內容以下: web

<?xml version="1.0" encoding="UTF-8"?>

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

	<context:component-scan base-package="com.mario.server.imp" />

</beans>

入門的內容因此刨除了令新手看不懂的屬性 ,context:component-scan 是自動包掃描 base-package是要掃描的包名字。這裏使用注入的方式,不然要寫bean,可是一旦這個包中bean 不少的時候掃描法會影響性能,bean的寫法就不說了,指定id,class什麼的你懂的。  spring

下一步須要導入spring 3所須要的包3.2版本(最後的pom.xml中有),若是缺包,在編譯時每每提示classNotfound根據那個類名,根據缺的類能夠在網上找類對應的jar包導入便可。 express

以後寫類 ,這裏個人類是UserServer,代碼以下: apache

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.mario.serverI.UserServerI;

@Service("userServer")
public class UserServerImp implements UserServerI{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UserServerImp.class);

	
    @Override
    public void test() {
    	// TODO Auto-generated method stub
    	logger.info("mssage from UserServer");
    }
}

這裏注意下 @Service這個註解,這個是注入中不可缺乏的,裏面的名字何時用?往下看。。。。 api

寫一個junit測試類,用來測試spring是否配置好。測試代碼以下,@test這個註解須要導入junit包: app

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mario.server.imp.UserServerImp;


public class mytest {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(mytest.class);
	

	@Test
	public void test(){
		
		ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
	 	UserServerImp us= (UserServerImp) ac.getBean("userServer");
	 	us.test();
	}
}

applicationContext對象用來獲取spring 的context ,以後經過context的getBean方法獲取UserService對象,getBean的參數userServer,就是UserServer類中@Server 裏面寫的那個userServer。 這裏它怎麼知道userServer就是UserServer中的註解並實例化UserServer類? 這是由於自動掃描包,在spring配置文件中指定了包含該類的包名,而後spring框架會掃描這個包中的類尋找@Server這個註解,若是有個值爲userServer的,spring就實例化了。 框架

測試代碼中能打印出test方法中的信息,spring 配置成功,在將其配置到web項目上,web.xml配置以下: dom

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
    <!-- spring 配置文件路徑 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  
  <!-- spring 監聽器 -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

Spring3 配置就完事了,接下來加struts2。 jsp

第一步仍是加包這個略,後面pom.xml裏面有,三個包,struts2-core,struts2-convension-plugin,struts2-spring-plugin,加了這三個後maven會自動加其餘須要的包。

stuts2的配置文件:

<?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>
	<!-- struts2的action必須放在一個指定的包空間下定義 -->
	<package name="basePackage" extends="struts-default"></package>

</struts>

package name 包名,不用注入的方式寫的話就是在這個標籤下寫action ,指定class什麼的,你懂的,不懂的能夠百度下。這裏由於注入因此很乾淨。

接下來寫action類,代碼以下:

import com.mario.serverI.UserServerI;

@ParentPackage("basePackage")
@Namespace("/")
@Action("userAction")
public class UserAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UserAction.class);
	
	private UserServerI userServer;
	
	
    public UserServerI getUserServer() {
		return userServer;
	}


    @Autowired
	public void setUserServer(UserServerI userServer) {
		this.userServer = userServer;
	}


	public void test(){
    	logger.info("message from struts2");
    	userServer.test();
    	
    }
   
    	
 
}


注意@ParentPackage這個註解,這裏面的base-package 就是配置文件中的package-name 指定的,@NameSpace是名字空間,Maven項目中默認用「/」

@Action就是這個類的訪問時用的名字,好比:localhost:8080/項目名/userAction!test.action 中的userAction 。

在這個Action中引用了userServer這個類,但test放法中並無實例化該類,怎麼作到的? 首先定義userServer類的get,set 方法,以後在set方法上加@AutoWirte註解就能夠了,很方便吧?

以後再web.xml中配置struts2,加入後的配置文件以下:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
    <!-- spring 配置文件路徑 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
   <!-- 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>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <!-- spring 監聽器 -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 加入了struts2的過濾器,filter 和filter-mapping。以後部署項目測試。localhost:8080/項目名/userAction!test.action。若是沒有問題會顯示空白,由於action中沒有result。

最後是Maven的pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myWebProJect</groupId>
  <artifactId>myWebProJect</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name></name>
  <description></description>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.annotation</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.ejb</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.weld</groupId>
      <artifactId>weld-osgi-bundle</artifactId>
      <version>1.0.1-SP3</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.servlet</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.1.4.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.common</groupId>
      <artifactId>hibernate-commons-annotations</artifactId>
      <version>4.0.1.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>1.0.1.Final</version>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.15.0-GA</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <version>3.1.0.GA</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.spec.javax.transaction</groupId>
      <artifactId>jboss-transaction-api_1.1_spec</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.1.4.Final</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-expression</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-orm</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>junit</groupId>
    	<artifactId>junit</artifactId>
    	<version>4.11</version>
    	<scope/>
    </dependency>
    <dependency>
    	<groupId>log4j</groupId>
    	<artifactId>log4j</artifactId>
    	<version>1.2.17</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-core</artifactId>
    	<version>2.3.15.1</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-spring-plugin</artifactId>
    	<version>2.3.15.1</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-convention-plugin</artifactId>
    	<version>2.3.15.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
相關文章
相關標籤/搜索