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

相關文章
相關標籤/搜索