慢性阻塞性肺疾病定义:SpringMVC_使用

来源:百度文库 编辑:中财网 时间:2024/05/11 18:08:22

SpringMVC_使用

1.@RequestMapping  用法 

SpringMVC中,@RequestMapping用来处理请求,比如XXX.do  

@RequestMapping("/aaa")//类级别,可以不需要,如果要了,下面所有的请求路径前都需要加入/aaa   
public class ccccontroller{

@RequestMapping("/bbb")
//方法级别,必须有,决定这个方法处理哪个请求,如果有类级别 /aaa/bbb
public String xxx()
{
//如果没有类级别的就直接请求/bbb
return;
}
}
  2:接收带参数的请求,接收用户请求参数值 
请求1: /test/start.do?name=zhangsan 
请求2: /test/start/zhangsan.do 

采用 URL模板 @RequestMapping("/start/{name}") //这个name 随便啥都可以 包含多个  @RequestMapping ("/start/{name}/{age}") 

3:不同请求方法,用不同处理方法   get  post 

   @RequestMapping (value="/start" ,method=RequestMethod.GET)//处理post 就换成 POST 

另外,如果Servlet想要做到/test/start/zhangsan.do这种请求的话需要 URL 重写才能做到,或者用 /start/* 作为 Servlet 请求控制器,在其中进行截取和判断。 
第二种方法:自己写一个 URL 重写的 Filter 
第三种方法:使用现成的 urlrewriter 工具 
第四种方法:使用 Web 服务器的 URL 重写功能
public String start(@PathVariable("name") string name){ //和上面的对应
return ;//方法体里面就可以直接获得参数
}

 
Servlet中用Filter重写示例 
web.xml中:  

  
xmlns=" http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Action
com.bao.servlet.Action


Action
/Action



action_name
com.bao.filter.ActionNameFilter

pattern
/action/[^/]+



action_name
/action/*

controller接收参数的使用

springmvc提供了@RequestParam注释帮助我们获取参数。

用法@RequestParam("接收的参数名")

例如:

@RequestMapping(params="servlet=login")
public String login(@RequestParam("username")String username,@RequestParam("password")String password,HttpServletRequest request,ModelMap map){
//处理登录逻辑,省略
return "success";
}

当客户端的URL提交了username参数,password参数,那么我们的Controller就可以接收并处理了。

要注意,提交的username参数和password参数不可以是null,即一定要传这两个参数,不然会抛异常。

另外,@RequestParam可以省略参数名,那么就会以它注释的变量名作为参数名。

 

视图名的确定。

spring mvc可以通过可以通过多种方式确定视图名,在前面的例子中,方法无返回值,视图名根据请求参数确定。Controller方法还是返回一个String类型的值作为视图名,如:

@RequestMapping("edit")

    public void edit(@RequestParam int id) {...}

则返回的视图为../edit.jsp

使用过低版本 Spring MVC 的读者都知道:当创建一个 Controller 时,我们需要直接或间接地实现 org.springframework.web.servlet.mvc.Controller 接口。一般情况下,我们是通过继承 SimpleFormController 或 MultiActionController 来定义自己的 Controller 的。在定义 Controller 后,一个重要的事件是在 Spring MVC 的配置文件中通过 HandlerMapping 定义请求和控制器的映射关系,以便将两者关联起来。

来看一下基于注解的 Controller 是如何定义做到这一点的,下面是使用注解的 BbtForumController:

package com.baobaotao.web;  
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Collection;
@Controller //<——①
@RequestMapping("/forum.do")
public class BbtForumController {
@Autowired
private BbtForumService bbtForumService;
@RequestMapping //<——②
public String listAllBoard() {
bbtForumService.getAllBoard();
System.out.println("call listAllBoard method.");
return "listBoard";
}
}

从上面代码中,我们可以看出 BbtForumController 和一般的类并没有区别,它没有实现任何特殊的接口,因而是一个地道的 POJO。让这个 POJO 与众不同的魔棒就是 Spring MVC 的注解!

在 ① 处使用了两个注解,分别是 @Controller 和 @RequestMapping。在“使用 Spring 2.5 基于注解驱动的 IoC”这篇文章里,笔者曾经指出过 @Controller、@Service 以及 @Repository 和 @Component 注解的作用是等价的:将一个类成为 Spring 容器的 Bean。由于 Spring MVC 的 Controller 必须事先是一个 Bean,所以 @Controller 注解是不可缺少的。

真正让 BbtForumController 具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解。@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流。在 ① 处,我们让 BbtForumController 关联“/forum.do”的请求,而 ② 处,我们具体地指定 listAllBoard() 方法来处理请求。所以在类声明处标注的 @RequestMapping 相当于让 POJO 实现了 Controller 接口,而在方法定义处的 @RequestMapping 相当于让 POJO 扩展 Spring 预定义的 Controller(如 SimpleFormController 等)。

为了让基于注解的 Spring MVC 真正工作起来,需要在 Spring MVC 对应的 xxx-servlet.xml 配置文件中做一些手脚。在此之前,还是先来看一下 web.xml 的配置吧:

  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
Spring Annotation MVC Sample


contextConfigLocation
classpath:applicationContext.xml




org.springframework.web.context.ContextLoaderListener




annomvc
org.springframework.web.servlet.DispatcherServlet

2


annomvc
*.do

web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示:

  
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">






p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

因为 Spring 所有功能都在 Bean 的基础上演化而来,所以必须事先将 Controller 变成 Bean,这是通过在类中标注 @Controller 并在 annomvc-servlet.xml 中启用组件扫描机制来完成的,如 ① 所示。

在 ② 处,配置了一个 AnnotationMethodHandlerAdapter,它负责根据 Bean 中的 Spring MVC 注解对 Bean 进行加工处理,使这些 Bean 变成控制器并映射特定的 URL 请求。

而 ③ 处的工作是定义模型视图名称的解析规则,这里我们使用了 Spring 2.5 的特殊命名空间,即 p 命名空间,它将原先需要通过 元素配置的内容转化为 属性配置,在一定程度上简化了 的配置。

启动 Tomcat,发送 http://localhost/forum.do URL 请求,BbtForumController 的 listAllBoard() 方法将响应这个请求,并转向 WEB-INF/jsp/listBoard.jsp 的视图页面。

在controller中,如果在某方法中需要重定向则 return "redirect:xx.html"(与类的RequestMapping的值在同一个路径下)