Spring MVC系列:(8)RequestMapping



RequestMapping

org.springframework.web.bind.annotation.RequestMapping

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. 



1、一個Action中,有多個業務方法


通過模塊根路徑 + 功能子路徑 = 訪問模塊下子功能的路徑

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.action

查詢用戶:http://127.0.0.1:8080/springmvc02/user/find.action


2、在業務控制方法中寫入普通變量收集參數


爲UserAction下add方法添加參數

@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";
    }
    
}


訪問如下地址:

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


3、限定業務方法的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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章