MVC參數傳遞


請求參數自動類型轉換

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陸</title>
</head>
<body>
<form action="/fourth/oneRequest" method="post">
    賬戶:<input type="text" name="userName"/>
    密碼:<input type="password" name="userpwd"/>
    <input type="submit" value="登陸"/>
</form>
</body>
</html>

(控制器Controller中的方法參數名稱必須和表單元素的name屬性值保持一致)

@Controller
@RequestMapping("/fourth")
public class FourthController {

    /**
     * 1、請求參數的自動類型轉換
     * @param userName
     * @param userpwd
     * @param model
     * @return
     * 控制器Controller中的方法參數名稱必須和表單元素的name屬性值保持一致
     */
    @RequestMapping(value = "/oneRequest")
    public String oneRequest(String userName,String userpwd, Model model){
        System.out.println(userName+"\t"+userpwd);
        model.addAttribute("userCode",userName);
        return "welcome";
    }

}

 

@RequestParam注解

jsp頁面

 

 

 Controller

RequestMethod.POST

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陸</title>
</head>
<body>
<form action="/fourth/twoRequest" method="post">
    賬戶:<input type="text" name="userName"/>
    密碼:<input type="password" name="userpwd"/>
    <input type="submit" value="登陸"/>
</form>
</body>
</html>

Controller

(此處必須設置請求類型,否則會顯示405錯誤)

/**
 * 2、RequestMethod.POST 此處必須設置請求類型 否則將會顯示405錯誤
 * @param userName
 * @param userpwd
 * @param model
 * @return
 *  控制器Controller中的方法參數名稱必須和表單元素的name屬性值保持一致
 */
@RequestMapping(value = "/twoRequest",method = RequestMethod.POST)
public String twoRequest(String userName,String userpwd, Model model){
    System.out.println(userName+"\t"+userpwd);
    model.addAttribute("userCode",userName);
    return "welcome";
}

RESTFUL風格的參數傳遞

 

 

 對象參數

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陸</title>
</head>
<body>
<form action="/fourth/Info" method="post">
    賬戶:<input type="text" name="userName"/>
    密碼:<input type="password" name="userpwd"/>
    <input type="submit" value="登陸"/>
</form>
</body>
</html>
/**
 * 5、對象參數
 */
@RequestMapping(value = "/Info")
public String UserRequest(UserInfo info){
    System.out.println(info.getUserName());
    return "welcome";
}

域屬性對象參數

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陸</title>
</head>
<body>
<form action="/fourth/userInfoRequest" method="post">
    teacher01:<input type="text" name="teacher.teacherName"/>
    <input type="submit" value="登陸"/>
</form>
</body>
</html>
/**
 * 6、域屬性對象參數
 */
@RequestMapping(value = "/userInfoRequest")
public String UserInfoRequest(UserInfo info){
    System.out.println(info.getTeacher().getTeacherName());
    return "welcome";
}

域屬性集合參數

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陸</title>
</head>
<body>
<form action="/fourth/userInfoRequest" method="post">
    teacher02:<input type="text" name="teacherList[0].teacherName"/>
    teacher03:<input type="text" name="teacherList[1].teacherName"/>
    <input type="submit" value="登陸"/>
</form>
</body>
</html>
/**
 * 7、域屬性集合參數
 */
@RequestMapping(value = "/userListRequest")
public String UserListRequest(UserInfo info){
    System.out.println(info.getTeacherList());
    return "welcome";
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM