一、stpringMVC+maven基础搭建篇

来源:互联网 发布:phpcms和织梦cms比较 编辑:程序博客网 时间:2024/06/05 03:12

转载自:http://www.cnblogs.com/xing901022/p/5240044.html

加上个人的一些补充
关于怎样配置maven这里就不说了,网上教程一大把。

需要的jar包

Spring framework spring-context
Spring framework spring-mvc
具体可以参考maven中的引用:

<dependencies><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>

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">     <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/applicationContext.xml</param-value>        <!-- 默认是/WEB-INF/applicationContext.xml -->     </context-param>     <listener>        <listener-class>            org.springframework.web.context.ContextLoaderListener        </listener-class>     </listener>    <servlet>        <servlet-name>SpringMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>            <!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>SpringMVC</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

其中,必要的配置就是指定servlet和listener.

ContextLoaderListener指定了IOC容器初始化的方法
DispatcherServlet则定义了mvc的相关内容,并配置拦截的url,如上面所示,所有/开头的请求,都会通过SpringMVC这个servlet进行处理。
他们都需要一个xml文件,默认位置上面已经说过了。

applicationContext.xml

空的,反正咱也没用什么bean。

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context" 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/tx                           http://www.springframework.org/schema/tx/spring-tx-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/context                           http://www.springframework.org/schema/context/spring-context-4.0.xsd"></beans>

SpringMVC-servlet.xml

里面放一个扫描controller的配置即可。

<?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="hello" /></beans>

controller文件

package hello;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class HelloController {    @RequestMapping("/hello")    public @ResponseBody String test() {        return "hello, world! This com from spring!";    }}

总结一下:

1 两个maven依赖,spring-context;spring-mvc。maven就会自动下载所有关联的jar包,包括

spring-webmvc
spring-beans
spring-core
spring-expression
spring-web
spring-context
spring-aop
aopalliance
commons-logging
2 一个web.xml文件,配置了listener和servlet
3 两个spring相关的文件,applicationContext.xml和servletName-servlet.xml
4 一个controller文件,配置了拦截的url处理代码

有了这些准备工作,运行后输入

de >http://localhost:8080/SpringTest/hellode>
就能得到

de >hello, world! This com from spring!de>
这样的信息,恭喜你的SpringMVC搭起来了!

如果你发现404,请确保你的META-INF/WEB-INF都是在src-main-webapp下

0 0
原创粉丝点击