• SpringMVC的各种注解


      @RequestMapping

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

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

    @RequestMapping(value="/*")

    1 就是拦截所有请求

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

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

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

    这样的请求。

     2 拦截多个(多个)请求

    例如:

     1 /**
     2      * 拦截多种请求
     3      *  如: @RequestMapping(value={"/index","/hello"})
     4      *  访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
     5      * @return
     6      * @throws SQLException
     7      */
     8     //@AuthPassport
     9     @RequestMapping(value={"/index","/hello"})
    10     public ModelAndView index() throws SQLException{
    11         
    12         //throw new SQLException("数据库异常。");
    13         
    14         ModelAndView modelAndView = new ModelAndView();  
    15         //在jsp中可以通过 ${message} 的形式来获取绑定的值
    16         modelAndView.addObject("message", "Hello World!");  
    17         modelAndView.setViewName("index");  
    18         return modelAndView;
    19     }

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

    获取到URL中的占位参数

    例如:

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

    明天继续填坑。

    项目所在位置如下图:

    BaseController.java:

     1 package com.demo.web.controllers;
     2 
     3 import java.sql.SQLException;
     4 import javax.servlet.http.HttpServletRequest;
     5 import org.springframework.web.bind.annotation.ExceptionHandler;
     6 /**
     7  * 
     8  * @author Wei
     9  * @time  2017年4月20日 下午3:56:51
    10  */
    11 public abstract class BaseController {  
    12 
    13     @ExceptionHandler
    14     public String exception(HttpServletRequest request, Exception e) {  
    15           
    16         request.setAttribute("exceptionMessage", e.getMessage());  
    17           
    18         // 根据不同的异常类型进行不同处理
    19         if(e instanceof SQLException) 
    20             return "testerror";   
    21         else
    22             return "error";  
    23     }  
    24     
    25 }  
    HelloWorldController.java
     1 package com.demo.web.controllers;
     2 
     3 import java.sql.SQLException;
     4 
     5 import org.springframework.stereotype.Controller;
     6 import org.springframework.web.bind.annotation.PathVariable;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestMethod;
     9 import org.springframework.web.servlet.ModelAndView;
    10 
    11 /**
    12  * 
    13  * @author Wei
    14  * @time  2017年4月20日 下午4:56:43
    15  */
    16 @Controller
    17 @RequestMapping(value = "/helloworld")
    18 public class HelloWorldController extends BaseController {
    19     /**
    20      * 拦截所有请求:
    21      * @RequestMapping(value="/*", method = {RequestMethod.GET})
    22      * http://localhost:8080/SpringMVCTag/helloworld/xd
    23      */
    24     //@AuthPassport
    25     @RequestMapping(value="/*", method = {RequestMethod.GET})
    26     public ModelAndView urlTest(){
    27         ModelAndView modelAndView = new ModelAndView();   
    28         //跳转到 urltest.jsp或者其他后缀的页面,这个要看springmvc的配置文件里是怎么设置的,
    29         modelAndView.setViewName("urltest");  
    30         return modelAndView;
    31     }
    32     /**
    33      * 拦截多种请求
    34      *  如: @RequestMapping(value={"/index","/hello"})
    35      *  访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
    36      * @return
    37      * @throws SQLException
    38      */
    39     //@AuthPassport
    40     @RequestMapping(value={"/index","/hello"})
    41     public ModelAndView index() throws SQLException{
    42         
    43         //throw new SQLException("数据库异常。");
    44         
    45         ModelAndView modelAndView = new ModelAndView();  
    46         //在jsp中可以通过 ${message} 的形式来获取绑定的值
    47         modelAndView.addObject("message", "Hello World!");  
    48         modelAndView.setViewName("index");  
    49         return modelAndView;
    50     }
    51     /**
    52      * @RequestMapping 结合 @PathVariable 注解可以获取URL中带占位的那个参数值,
    53      * 具体到这个例子中,在方法名中Integer id这个入参的值就是URL中的值,
    54      * 假如URL为:
    55      * http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan
    56      * 那么在跳转到的detail.jsp中通过
    57      * ${cxrr}的形式就可以获取到 yangdandan 这个字符串
    58      * @param id
    59      * @return
    60      */
    61     @RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET})
    62     public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){
    63         
    64         ModelAndView modelAndView = new ModelAndView();  
    65         modelAndView.addObject("crxx", thepara);  
    66         modelAndView.setViewName("detail");  
    67         return modelAndView;
    68     }
    69     
    70     @RequestMapping(value="/reg/{name:\w+}-{age:\d+}", method = {RequestMethod.GET})
    71     public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){
    72         
    73         ModelAndView modelAndView = new ModelAndView();   
    74         modelAndView.addObject("name", name); 
    75         modelAndView.addObject("age", age); 
    76         modelAndView.setViewName("regurltest");  
    77         return modelAndView;
    78     }
    79     
    80     @RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})
    81     public ModelAndView paramsTest(){
    82         
    83         ModelAndView modelAndView = new ModelAndView();   
    84         modelAndView.setViewName("paramstest");  
    85         return modelAndView;
    86     }
    87     
    88 }
  • 相关阅读:
    MySQL——UPDATE更新
    MySQL——WHERE字句
    MySQL——查询数据
    MySQL——删除数据表
    MySQL——插入数据
    MySQL——创建数据表
    MySQL——数据类型
    MySQL——选择数据库
    MySQL——删除数据库
    MySQL——创建数据库
  • 原文地址:https://www.cnblogs.com/Sunnor/p/6739945.html
Copyright © 2020-2023  润新知