ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MVC参数传递

2019-11-10 13:01:04  阅读:213  来源: 互联网

标签:userName return String param 参数传递 MVC 参数 userpwd


请求参数自动类型转换

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

 

标签:userName,return,String,param,参数传递,MVC,参数,userpwd
来源: https://www.cnblogs.com/mayuan01/p/11829565.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有