SpringMvc项目分析

         首先在配置文件中配置一个视图解析器,视图解析器规定了视图解析的规则,即controller处理请求之后,返回给DispatcheServlet一个字符串(也可能是ModelAndView之类的),而DispatcheServlet又将字符串传递给视图解析器,不同的视图解析器会作出不同的处理,从而映射到不同的视图上,并进行渲染:

<?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:context="http://www.springframework.org/schema/context"
       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.xsd">
       <!--配置自动扫描的包-->
       <context:component-scan base-package="com.seven.demos"></context:component-scan>

       <!--配置视图解析器,将视图逻辑名解析为/WEB-INF/pages/<viewName>.jsp-->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/WEB-INF/pages/"/>
             <property name="suffix" value=".jsp"/>
      </bean>
</beans>

处理器类如下:

package com.seven.demos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by hu on 2016/4/2.
 */
/*
* 创建处理器类
* */
@Controller
public class Hello {
    @RequestMapping("/login")
    public String hello(){
        System.out.println("enter the hello method...");
        return  "hu";
    }
}

运行项目,那么在浏览器中输入"http://localhost:8082/webapp/login",就会访问/WEB-INF/pages/hu.jsp这个文件。

      由上面控制器的代码可以看出,SpringMvc使用@RequestMapping注解为控制器指定可以处理哪些URL请求,这个注解在类定义处和方法定义处都可标注,

           -在类处定义:提供初步的请求映射信息,相对于WEB应用的根目录。

           -在方法处定义:提供进一步的细分信息。相对于类定义处的URL,若类定义处未标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录。

      DispatcheServlet截获请求之后,就通过控制器上@RequestMapping提供的映射信息确定请求所对应的处理方法。

      @RequestMapping除了可以使用请求URL映射请求外,还可以使用请求方法,请求参数及请求头映射请求。@RequestMapping的value,method,params及heads分别表示请求URL,请求方法,请求参数以及请求头的映射条件,它们之间是与的关系,联合使用多个条件可以让请求映射更加精确,具体用法如下:

package com.seven.demos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by hu on 2016/4/2.
 */
@RequestMapping("/SpringMvc")
@Controller
public class SpringMvcTest {
    private final String SUCCESS="success";

    //  @RequestMapping还支持Ant风格的URL
    //    /user/*/createUser 匹配如: /user/aa/createUser、 /user/bb/createUser
    //    /user/**/createUser 匹配如: /user/aa/bb/createUser、/user/createUser
    //    /user/createUser?? 匹配如:/user/createUseraa、/user/createUserbb

    //test1()处理的是来自http://localhost:8082/webapp/SpringMvc/delete的POST请求,并且带有一个名为userId的参数
    //如果带有多个参数,可以使用{"param1","param2"},当然也支持简单的表达式,{"param1=value1","param2"},即请求中
    //必须包含两个参数,param1,param2,并且param1的值必须为value1
    @RequestMapping(value="/delete",method = RequestMethod.POST,params = "userId")
    public String test1(){
        return "test1";
    }
    /*
    * @PathVariable可以将URL中占位符参数绑定到控制器处理方法的入参中.
    * */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        return SUCCESS;
    }

}

  

原文地址:https://www.cnblogs.com/hujingwei/p/5346971.html