用intellig IDEA开发 Spring MVC第一篇

来源:互联网 发布:mysql in 值少会索引 编辑:程序博客网 时间:2024/06/07 07:18

五年没搞后台了,今天重温一下, 工具也换了 不用eclipse了

按照工程创建的向导  新建一个工程 spring mvc



创建完成以后的目录结构有问题, 把lib放到 WEB-INF目录下就可以了


先看一下 web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app 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"         version="3.1">    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>*.form</url-pattern>    </servlet-mapping></web-app>

先说一下DispatcherServlet 这个servlet会拦截所有匹配到的*.form的请求, 

其中

 <load-on-startup>1</load-on-startup>

load-on-startup:表示启动容器时初始化该Servlet;

 <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>        </init-param>

init-param 这个参数可以不用写

该DispatcherServlet默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”。

其中[servlet名字]-servlet.xml 因为定义的servlet名字为dispatcher 所以默认会加载 /WEB-INF/dispatcher-servlet.xml这个文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.0.xsd">  <span style="white-space:pre"></span>    <context:component-scan base-package="coffee.spring.controler"/></beans>
注意xml需要添加命名空间 

xmlns:context="http://www.springframework.org/schema/context"

以及sche
schemaLocation

<context:component-scan base-package="coffee.spring.controler"/>
程序会扫描包下面的class文件。

也可以修改文件的路径

因此我们可以通过添加初始化参数     <servlet>        <servlet-name>chapter2</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-servlet-config.xml</param-value>        </init-param>    </servlet> 

如果使用如上配置,Spring Web MVC框架将加载“classpath:spring-servlet-config.xml”来进行初始化上下文而不是“/WEB-INF/[servlet名字]-servlet.xml”。

接着看一下Contaoller

package coffee.spring.controler;

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * Created by coffee on 2016/11/5. */@Controller@RequestMapping("hello")public class HelloController {    @RequestMapping(value="world",method = RequestMethod.GET)    public @ResponseBody String printHello() {        return "hello";    }}

访问的时候 直接

http://localhost:8080/hello/world.form访问。 

注意 @ResponseBody  

0 0