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/

项目结构

Maven Spring MVC 实践_第1张图片

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.0modelVersion>
    <groupId>com.warrior.studygroupId>
    <artifactId>SpringMVCartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>SpringMVC Maven Webappname>
    <url>http://maven.apache.orgurl>
    <properties>
        <org.slf4j-version>1.6.1org.slf4j-version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>3.1.1.RELEASEversion>
            <exclusions>
                
                <exclusion>
                    <groupId>commons-logginggroupId>
                    <artifactId>commons-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>3.1.1.RELEASEversion>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${org.slf4j-version}version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>${org.slf4j-version}version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>${org.slf4j-version}version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.16version>
            <scope>runtimescope>
        dependency>

    dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.6source>
                    <target>1.6target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.mortbay.jettygroupId>
                <artifactId>maven-jetty-pluginartifactId>
                <version>6.1.26version>
                <configuration>
                    <scanIntervalSeconds>10scanIntervalSeconds>
                    <webApp>
                        <contextPath>/mapsourcecontextPath>
                    webApp>
                    <webDefaultXml>src/main/resources/webdefault.xmlwebDefaultXml>
                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 Applicationdisplay-name>
 7   
 8   <servlet>
 9         <servlet-name>appServletservlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
11         <init-param>
12             <param-name>contextConfigLocationparam-name>
13             <param-value>classpath:spring/servlet-context.xmlparam-value>
14         init-param>
15         <load-on-startup>1load-on-startup>
16     servlet>        
17  
18     <servlet-mapping>
19         <servlet-name>appServletservlet-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     
12  
13     
14     
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

你可能感兴趣的:(Maven Spring MVC 实践)