SpringMVC的各种注解

时间:2023-03-09 20:43:57
SpringMVC的各种注解

  @RequestMapping

SpringMVC的各种注解

这个注解标注在方法名上,如

 /**
* 拦截所有请求:
* @RequestMapping(value="/*", method = {RequestMethod.GET})
* http://localhost:8080/SpringMVCTag/helloworld/xd
*/
//@AuthPassport
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
//跳转到 urltest.jsp或者其他后缀的页面,这个要看springmvc的配置文件里是怎么设置的,
modelAndView.setViewName("urltest");
return modelAndView;
}

@RequestMapping(value="/*")

1 就是拦截所有请求

结合上面的一小段代码就是拦截:

类似于http://localhost:8080/SpringMVCTag/helloworld/我是任意字符,

http://localhost:8080/SpringMVCTag/helloworld/comaosdfjoa

这样的请求。

2 拦截多个(多个)请求

例如:

 /**
* 拦截多种请求
* 如: @RequestMapping(value={"/index","/hello"})
* 访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
* @return
* @throws SQLException
*/
//@AuthPassport
@RequestMapping(value={"/index","/hello"})
public ModelAndView index() throws SQLException{ //throw new SQLException("数据库异常。"); ModelAndView modelAndView = new ModelAndView();
//在jsp中可以通过 ${message} 的形式来获取绑定的值
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("index");
return modelAndView;
}

3 @RequestMapping 注解结合 @PathVariable注解可以在控制器(也就是具体的方法)的入参里

获取到URL中的占位参数

例如:

 /**
* @RequestMapping 结合 @PathVariable 注解可以获取URL中带占位的那个参数值,
* 具体到这个例子中,在方法名中Integer id这个入参的值就是URL中的值,
* 假如URL为:
* http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan
* 那么在跳转到的detail.jsp中通过
* ${cxrr}的形式就可以获取到 yangdandan 这个字符串
* @param id
* @return
*/
@RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET})
public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("crxx", thepara);
modelAndView.setViewName("detail");
return modelAndView;
}

明天继续填坑。

项目所在位置如下图:

SpringMVC的各种注解

BaseController.java:

 package com.demo.web.controllers;

 import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
*
* @author Wei
* @time 2017年4月20日 下午3:56:51
*/
public abstract class BaseController { @ExceptionHandler
public String exception(HttpServletRequest request, Exception e) { request.setAttribute("exceptionMessage", e.getMessage()); // 根据不同的异常类型进行不同处理
if(e instanceof SQLException)
return "testerror";
else
return "error";
} }
HelloWorldController.java
 package com.demo.web.controllers;

 import java.sql.SQLException;

 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;
import org.springframework.web.servlet.ModelAndView; /**
*
* @author Wei
* @time 2017年4月20日 下午4:56:43
*/
@Controller
@RequestMapping(value = "/helloworld")
public class HelloWorldController extends BaseController {
/**
* 拦截所有请求:
* @RequestMapping(value="/*", method = {RequestMethod.GET})
* http://localhost:8080/SpringMVCTag/helloworld/xd
*/
//@AuthPassport
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
//跳转到 urltest.jsp或者其他后缀的页面,这个要看springmvc的配置文件里是怎么设置的,
modelAndView.setViewName("urltest");
return modelAndView;
}
/**
* 拦截多种请求
* 如: @RequestMapping(value={"/index","/hello"})
* 访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
* @return
* @throws SQLException
*/
//@AuthPassport
@RequestMapping(value={"/index","/hello"})
public ModelAndView index() throws SQLException{ //throw new SQLException("数据库异常。"); ModelAndView modelAndView = new ModelAndView();
//在jsp中可以通过 ${message} 的形式来获取绑定的值
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("index");
return modelAndView;
}
/**
* @RequestMapping 结合 @PathVariable 注解可以获取URL中带占位的那个参数值,
* 具体到这个例子中,在方法名中Integer id这个入参的值就是URL中的值,
* 假如URL为:
* http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan
* 那么在跳转到的detail.jsp中通过
* ${cxrr}的形式就可以获取到 yangdandan 这个字符串
* @param id
* @return
*/
@RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET})
public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("crxx", thepara);
modelAndView.setViewName("detail");
return modelAndView;
} @RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", name);
modelAndView.addObject("age", age);
modelAndView.setViewName("regurltest");
return modelAndView;
} @RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})
public ModelAndView paramsTest(){ ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("paramstest");
return modelAndView;
} }