首先在配置文件中配置一个视图解析器,视图解析器规定了视图解析的规则,即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;
} }

  

最新文章

  1. pg gem 安装(postgresql94)
  2. Windows下Nginx Virtual Host多站点配置详解
  3. 如何在Linux服务器中隐藏PHP版本
  4. DB2 runstats、reorgchk、reorg 命令
  5. 使用bootstrap和metroui设计的微网站或手机app界面
  6. Android堆栈分析
  7. Android-判断当前网络是否可用
  8. php验证输入字符串中含有非法字符
  9. 存储过程&amp;Function
  10. 利用多线程资源竞争技术上传shell
  11. INNO setup 制作安装包
  12. useBean
  13. SMM框架--maven创建web项目
  14. virtual和abstract区别
  15. jfinal 项目 控制层、ORM层、AOP层,在发表之前一定要记得保存
  16. 卸载WPS后怎么WORD的图标还是WPS
  17. kubeadm安装K8S单master双节点集群
  18. Apache+php+mysql环境配置
  19. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏
  20. 职责链模式c#(处理车)

热门文章

  1. EBS 资源路径
  2. Freemarker的第二次使用~list的元素是数组
  3. Bootstrap 模态框插件
  4. windbg sos版本不匹配问题解决
  5. JQuery源码之“名叫extend的继承”
  6. YUM Installation PostgreSQL
  7. MoSCoW Method
  8. iOS FMDB小试了一下
  9. sell - 配置service
  10. web前端学习网址