springMVC demo搭建
1、使用idea新建一个基于maven的web项目,参考 http://www.cnblogs.com/winkey4986/p/5279820.html2、采取了比较偷懒的配置方法,只配置了一个DispatcherServlet,注意区别同时配置DispatcherServlet和ContextLoaderListener
(从长远考虑是需要同时配置的,DispatcherServelet中主要扫描Controller,参考 http://www.cnblogs.com/winkey4986/p/5280138.html)
3、先配置pom.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.icebean</groupId>
<artifactId>chicken</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>chicken Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--jetty-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.6</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9999</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
<finalName>chicken</finalName>
</build>
</project>
pom中配置了 spring-webmvc、spring-context,另外插件中配置了jetty web容器,指定端口为9999
4、“偷懒配法”web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>chicken</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
对于web.xml中指定的spring配置文件applicationContext.xml(此文件放在resources中)配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.icebean"/>
</beans>
5、ok。配置文件好了,接下来是代码:
package com.icebean.demo;
/**
* Created by ***on 2016/3/15.
*/
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String test() {
return "hello, world! This com from spring!";
}
}
然后运行maven中的jetty插件,访问地址:http://localhost:9999/chicken/hello 搞定
总结一下:
1 创建一个标准的基于maven的web项目
2 两个maven依赖,spring-context;spring-mvc。maven就会自动下载所有关联的jar包,包括
[*]spring-webmvc
[*]spring-beans
[*]spring-core
[*]spring-expression
[*]spring-web
[*]spring-context
[*]spring-aop
[*]aopalliance
[*]commons-logging
3 一个web.xml文件,配置了listener和servlet(可以只有一个DispatcherServelet)
4 两个spring相关的文件,applicationContext.xml和servletName-servlet.xml(可以只有一个applicationContext)
5 一个controller文件,配置了拦截的url处理代码
页:
[1]