Spring MVC系列:(8)RequestMapping



RequestMappingjava

org.springframework.web.bind.annotation.RequestMappingweb

Annotation for mapping web requests onto specific handler classes and/or handler methods. Provides a consistent style between Servlet and Portlet environments, with the semantics adapting to the concrete environment. spring



一、一个Action中,有多个业务方法服务器


经过模块根路径 + 功能子路径 = 访问模块下子功能的路径mvc

package com.rk.action;

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

@Controller
@RequestMapping(value="/user")
public class UserAction {

    @RequestMapping(value="/add")
    public String add(Model model) throws Exception{
        model.addAttribute("message", "添加用户");
        return "/success.jsp";
    }
 
    @RequestMapping(value="/find")
    public String find(Model model) throws Exception{
        model.addAttribute("message", "查找用户");
        return "/success.jsp";
    }
    
}


增长用户:http://127.0.0.1:8080/springmvc02/user/add.actionapp

查询用户:http://127.0.0.1:8080/springmvc02/user/find.actionjsp


二、在业务控制方法中写入普通变量收集参数ide


为UserAction下add方法添加参数
ui

@Controller
@RequestMapping(value="/user")
public class UserAction {

    @RequestMapping(value="/add")
    public String add(Model model, int id,String name, Double sal) throws Exception{
        System.out.println(id + " - " + name + " - " + sal);
        model.addAttribute("message", "添加用户");
        return "/success.jsp";
    }
 
    @RequestMapping(value="/find")
    public String find(Model model) throws Exception{
        model.addAttribute("message", "查找用户");
        return "/success.jsp";
    }
    
}


访问以下地址:spa

http://127.0.0.1:8080/springmvc02/user/add.action?id=1&name=Tomcat&sal=8000

服务器控制台会输出:

1 - Tomcat - 8000.0


若是不添加参数直接访问/user/add.action,则会报错

http://127.0.0.1:8080/springmvc02/user/add.action

后台错误信息以下:

Optional int parameter 'id' is not present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.

wKiom1fgZejRUfVwAAHsIG5liTA632.png


稍作修改,将int类型修改成Integer

    @RequestMapping(value="/add")
    public String add(Model model, Integer id,String name, Double sal) throws Exception{
        System.out.println(id + " - " + name + " - " + sal);
        model.addAttribute("message", "添加用户");
        return "/success.jsp";
    }

再次访问

http://127.0.0.1:8080/springmvc02/user/add.action

控制台输出:

null - null - null


三、限定业务方法的GET或POST请求方式

能够在业务控制方法前,指明该业务控制方法只能接收GET或POST的请求

    //@RequestMapping(value="/add",method={RequestMethod.POST})
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(Model model, Integer id,String name, Double sal) throws Exception{
        System.out.println(id + " - " + name + " - " + sal);
        model.addAttribute("message", "添加用户");
        return "/success.jsp";
    }

若是不书写method=RequestMethod.POST的话,GET和POST请求都支持

wKiom1fgaBSwy-oPAACAc7TsMKs000.png

相关文章
相关标签/搜索