Maven Spring MVC 实践

http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/

http://blog.springsource.com/2011/01/17/green-beans-getting-started-with-maven-and-spring/

项目结构

image

pod.xml

 

<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>com.warrior.study</groupId>
    <artifactId>SpringMVC</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringMVC Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <org.slf4j-version>1.6.1</org.slf4j-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.1.RELEASE</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- log -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/mapsource</contextPath>
                    </webApp>
                    <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

 

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6   <display-name>Archetype Created Web Application</display-name>
 7   
 8   <servlet>
 9         <servlet-name>appServlet</servlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11         <init-param>
12             <param-name>contextConfigLocation</param-name>
13             <param-value>classpath:spring/servlet-context.xml</param-value>
14         </init-param>
15         <load-on-startup>1</load-on-startup>
16     </servlet>        
17  
18     <servlet-mapping>
19         <servlet-name>appServlet</servlet-name>
20         <url-pattern>/</url-pattern>
21     </servlet-mapping>
22   
23 </web-app>

servlet-context.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         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     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
12  
13     <!-- Scans within the base package of the application for @Components to configure as beans -->
14     <!-- @Controller, @Service, @Configuration, etc. -->
15     <context:component-scan base-package="com.warrior" />
16     <mvc:annotation-driven />
17  
18 </beans>
 1 package com.warrior;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class HomeController {
 8 
 9     @RequestMapping(value = "/")
10     public String home() {
11         return "/index.jsp";
12     }
13 }

webdefault.xml 是jetty的配置文件

http://docs.codehaus.org/display/JETTY/webdefault.xml

posted @ 2012-05-09 10:44  骨头  阅读(10205)  评论(0编辑  收藏  举报