maven+spring mvc初尝试

只是一个可以运行的例子,俺们来看看。

目录结构:

pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.xinziruo.springmvctest</groupId>
  5   <artifactId>springmvctest</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>springmvctest Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <properties>
 11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 12         <java-version>1.6</java-version>
 13   </properties>
 14   <dependencies>
 15       <dependency>
 16             <groupId>org.springframework</groupId>
 17             <artifactId>spring-orm</artifactId>
 18             <version>3.1.1.RELEASE</version>
 19     </dependency>
 20     <dependency>
 21             <groupId>org.springframework</groupId>
 22             <artifactId>spring-web</artifactId>
 23             <version>3.1.1.RELEASE</version>
 24     </dependency>
 25 
 26     <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-webmvc</artifactId>
 29             <version>3.1.1.RELEASE</version>
 30     </dependency>
 31     <dependency>
 32       <groupId>junit</groupId>
 33       <artifactId>junit</artifactId>
 34       <version>3.8.1</version>
 35       <scope>test</scope>
 36     </dependency>
 37   </dependencies>
 38   <repositories>
 39         <!-- For testing against latest Spring snapshots -->
 40         <repository>
 41             <id>org.springframework.maven.snapshot</id>
 42             <name>Spring Maven Snapshot Repository</name>
 43             <url>http://maven.springframework.org/snapshot</url>
 44             <releases>
 45                 <enabled>false</enabled>
 46                 <updatePolicy>never</updatePolicy>
 47             </releases>
 48             <snapshots>
 49                 <updatePolicy>never</updatePolicy>
 50                 <enabled>true</enabled>
 51             </snapshots>
 52         </repository>
 53         <!-- For developing against latest Spring milestones -->
 54         <repository>
 55             <id>org.springframework.maven.milestone</id>
 56             <name>Spring Maven Milestone Repository</name>
 57             <url>http://maven.springframework.org/milestone</url>
 58             <snapshots>
 59                 <updatePolicy>never</updatePolicy>
 60                 <enabled>false</enabled>
 61             </snapshots>
 62         </repository>
 63         <repository>
 64             <id>sonatype-snapshots</id>
 65             <name>Sonatype Snapshots Repository</name>
 66             <url>http://oss.sonatype.org/content/repositories/snapshots/</url>
 67             <snapshots>
 68                 <enabled>true</enabled>
 69             </snapshots>
 70         </repository>
 71 
 72         <repository>
 73             <id>sonatype-releases</id>
 74             <name>Sonatype Releases Repository</name>
 75             <url>http://oss.sonatype.org/content/repositories/releases/</url>
 76         </repository>
 77     </repositories>
 78   <build>
 79         <plugins>
 80             <plugin>
 81                 <groupId>org.apache.maven.plugins</groupId>
 82                 <artifactId>maven-compiler-plugin</artifactId>
 83                 <version>2.3.2</version>
 84                 <configuration>
 85                     <source>${java-version}</source>
 86                     <target>${java-version}</target>
 87                 </configuration>
 88             </plugin>
 89             <plugin>
 90                 <groupId>org.apache.maven.plugins</groupId>
 91                 <artifactId>maven-war-plugin</artifactId>
 92                 <version>2.1.1</version>
 93                 <configuration>
 94                     <warName>springmvctest</warName>
 95                     <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
 96                 </configuration>
 97             </plugin>
 98             <plugin>
 99                 <groupId>org.apache.maven.plugins</groupId>
100                 <artifactId>maven-dependency-plugin</artifactId>
101                 <executions>
102                     <execution>
103                         <id>install</id>
104                         <phase>install</phase>
105                         <goals>
106                             <goal>sources</goal>
107                         </goals>
108                     </execution>
109                 </executions>
110             </plugin>
111 
112             <plugin>
113                 <groupId>org.apache.maven.plugins</groupId>
114                 <artifactId>maven-surefire-plugin</artifactId>
115                 <version>2.7.1</version>
116                 <configuration>
117                     <junitArtifactName>junit:junit</junitArtifactName>
118                     <excludes>
119                         <exclude>**/*_Roo_*</exclude>
120                     </excludes>
121                 </configuration>
122             </plugin>
123 
124             <plugin>
125                 <groupId>org.mortbay.jetty</groupId>
126                 <artifactId>maven-jetty-plugin</artifactId>
127                 <version>6.1.26</version>
128                 <configuration>
129                     <stopKey>foo</stopKey>
130                     <stopPort>9999</stopPort>
131                     <webAppConfig> 
132                         <contextPath>/springmvctest</contextPath> 
133                     </webAppConfig>
134                 </configuration>
135                 <executions>
136                     <execution>
137                         <id>start-jetty</id>
138                         <phase>pre-integration-test</phase>
139                         <goals>
140                             <goal>run</goal>
141                         </goals>
142                         <configuration>
143                             <scanIntervalSeconds>0</scanIntervalSeconds>
144                             <daemon>true</daemon>
145                         </configuration>
146                     </execution>
147                 </executions>
148             </plugin>
149         </plugins>
150     </build>
151 </project>
View Code

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 3     <context-param>
 4         <param-name>contextConfigLocation</param-name>
 5         <param-value>/WEB-INF/spring/root-context.xml</param-value>
 6     </context-param>
 7     
 8     <listener>
 9         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10     </listener>
11     
12     <servlet>
13     <!--springmvc的核心是DispatcherServlet,它负责控制整个页面的请求路径-->  
14        <servlet-name>appServlet</servlet-name>
15        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16        <!--初始化参数>/WEB-INF/classes/相当于src目录-->  
17        <init-param>
18           <!-- 这个param-name必须是contextConfigLocation-->  
19          <param-name>contextConfigLocation</param-name>
20          <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
21        </init-param>
22        <load-on-startup>1</load-on-startup>
23      </servlet>
24      
25      <servlet-mapping>
26      <!--拦截所有以do结尾的请求-->  
27        <servlet-name>appServlet</servlet-name>
28        <url-pattern>/</url-pattern>
29       </servlet-mapping>
30       
31       <!--处理从页面传递中文到后台而出现的中文乱码问题-->  
32       <filter>  
33         <filter-name>encodingFilter</filter-name>  
34         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
35         <init-param>  
36             <param-name>encoding</param-name>  
37             <param-value>UTF-8</param-value>  
38         </init-param>  
39          </filter>  
40     
41          <filter-mapping>  
42         <filter-name>encodingFilter</filter-name>  
43         <url-pattern>/*</url-pattern>  
44       </filter-mapping> 
45           
46       <welcome-file-list>  
47         <welcome-file>index.jsp</welcome-file>  
48       </welcome-file-list> 
49       
50 </web-app>
View Code

servlet-context.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans:beans xmlns="http://www.springframework.org/schema/mvc"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:beans="http://www.springframework.org/schema/beans"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10 
11     <context:component-scan base-package="com.xinziruo.springmvctest.controller" />
12     
13     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
14     
15     <!-- Enables the Spring MVC @Controller programming model -->
16     <annotation-driven />
17 
18     <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
19     <resources mapping="/resources/**" location="/resources/" />
20 
21 
22     <beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" id="viewResolver">
23         <beans:property name="ignoreAcceptHeader" value="true"></beans:property>
24         
25         <beans:property name="mediaTypes">
26             <beans:map>
27                 <beans:entry key="json" value="application/json"></beans:entry>
28                 <beans:entry key="xml" value="application/xml"></beans:entry>
29             </beans:map>
30         </beans:property>
31         
32         <beans:property name="defaultContentType" value="text/html"></beans:property>
33         
34         <beans:property name="favorParameter" value="false"></beans:property>
35         
36           <!--定义视图通过internalResourceView来表示使用的是Servlet/jsp技术-->  
37         <beans:property name="viewResolvers">
38             <beans:list>
39                 <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
40                     <beans:property name="order" value="1"></beans:property>
41                         <!--jsp存放的目录-->  
42                     <beans:property name="prefix" value="/jsp/" />
43                         <!--jsp文件的后缀-->  
44                     <beans:property name="suffix" value=".jsp" />
45                 </beans:bean>
46             </beans:list>
47         </beans:property>
48             
49     
50     </beans:bean>
51     
52 </beans:beans>
View Code

TestController.java

 1 package com.xinziruo.springmvctest.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 @Controller
 9 public class TestController {
10     @RequestMapping(value = "/", method = RequestMethod.GET)
11     String index(Model model){
12         model.addAttribute("msg","你好,~~ 妹纸~~");
13         return "test";
14     }
15     @RequestMapping(value = "/test", method = RequestMethod.GET)
16     String test(Model model){
17         model.addAttribute("msg","这是一个spring mvc 测试的例子");
18         return "test";
19     }
20 }
View Code

 test.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>测试,你懂的</title>
 8 </head>
 9 <body>
10 ${msg }
11 </body>
12 </html>
View Code

 

posted @ 2013-12-25 16:09  蓦然回首的包子  阅读(281)  评论(0)    收藏  举报