1. 程式人生 > >maven+Spring簡單web專案demo

maven+Spring簡單web專案demo

1.右鍵新建專案(沒有maven專案時,點選other),如下圖所示,選擇Maven Progect

2.設定專案儲存位置


3.選擇maven-archetype-webapp


4、輸入專案組id和本專案id,點選finish完成


5、專案完成後,右鍵專案屬性properties,選擇deploymentAssembly,將不需要釋出的目錄remove移除掉


6、最終專案目錄如下所示



7、修改maven的pom.xml檔案,增加jar包的依賴描述,maven會自動下載相關jar包及其依賴包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>exam</groupId>
  <artifactId>exam_2</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Webapp Archetype</name>
  <url>http://maven.apache.org</url>
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.0.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>
    
  </dependencies>
  <build>
    <finalName>exam_2</finalName>
  </build>
</project>

8、修改web.xml,配置spring和servlet

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

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:conf/applicationContext.xml</param-value>
	</context-param>

	<listener>

		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>exam</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>exam</param-name>
			<!-- 預設路徑在WebConte下面,預設名稱就是spring-servlet.xml,我們這裡就不使用預設值 -->
			<param-value>classpath:conf/exam-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>exam</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

</web-app>

9、資原始檔applicationContext.xml,因為我們只是簡單的使用spring,暫時沒有配其他的,但也要有該檔案,內容可以不填,基本的如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>

10、exam-servlet.xml配置對servlet的處理,檔案內容如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean頭部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <!-- 啟用@Controller模式 -->
    <mvc:annotation-driven />
    <!-- 對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 需要更改 -->
    <context:component-scan base-package="com.xiaoma.action" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix"><!-- 檢視檔案的字首 -->
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix"><!-- 檢視檔案的字尾 -->
            <value>.jsp</value>
        </property>
    </bean>
</beans>

11、現在我們可以新建一個action來測試一下,我們的spring是否可以正常使用,根據之前的字首及字尾配置,我們在WEB-INF下新建jsp/demo目錄,在demo目錄下新建       demo.jsp頁面

新建包com.xiaoma.action,在該包下新建TestAction.java類,

package com.xiaoma.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestAction {
	
	@RequestMapping("/test.do")
	public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
		System.out.println("1234");
		return  new ModelAndView("/demo/demo","message","abc");
	}

}

訪問test.do 時,即可返回demo.jsp頁面,並將message物件帶過去