java调用视图如何传参_Spring MVC中 视图 向 控制器 传参(接收方式)

本文详细介绍了SpringMVC中通过HttpServletRequest、方法参数名匹配及封装对象接收请求参数的三种方法,并提供了示例代码。通过HttpServletRequest获取参数,使用@RequestParam注解匹配参数名,以及通过对象属性名匹配参数,分别展示了不同接收方式的用法。同时,文章还提及了日期格式转换的问题及其解决方案。
摘要由CSDN通过智能技术生成

1、方法一(通过HttpServletRequest方式接收)

1.1、添加servlet的jar包

javax.servlet

javax.servlet-api

3.1.0

provided

1.2、spring.xml以及web.xml配置如上一章所示

1.3、创建index.jsp和success.jsp

Hello World!

test

Created by IntelliJ IDEA.

User: Mr Wei

Date: 2020/*/*

Time: 15:19

To change this template use File | Settings | File Templates.

--%>

Title

测试成功!

1.4、添加Controller类

package com.fan.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller

public class Controller1 {

@RequestMapping("/user")

public String test2(HttpServletRequest request){

String u = request.getParameter("username");

String p = request.getParameter("password");

System.out.println(u+" "+p);

return "success";

}

}

1.5、启动tomcat进行测试

2、方法二(页面传值时的key=处理请求的方法的参数名)

2.1、jar包的添加,spring.xml以及web.xml配置如上一章所示

2.2、创建index.jsp和success.jsp

Hello World!

test

Created by IntelliJ IDEA.

User: Mr Wei

Date: 2020/*/*

Time: 15:19

To change this template use File | Settings | File Templates.

--%>

Title

测试成功!

2.3、添加Controller类

package com.fan.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class Controller1 {

@RequestMapping("/user")

//传入的值 即username要和前端传入的key值相同。页面传值时的key=处理请求的方法的参数名

public String test1(String username){

System.out.println("传入的用户信息是:"+username);

return "success";

}

}

2.4、启动tomcat进行测试(2种测试方式)

2.4.2、点击index.jsp页面的test链接,也可以实现相应的跳转。

2种测试方式控制台结果如下如所示

80c97d2f3e90

测试结果

3、方法三(封装成对象,使用控件名和对象的属性名一致的方式进行接收)

3.1、jar包的添加,spring.xml以及web.xml配置如上一章所示

3.2、创建index.jsp和success.jsp

Hello World!

1

2

3

4

5

Created by IntelliJ IDEA.

User: Mr Wei

Date: 2020/*/*

Time: 15:19

To change this template use File | Settings | File Templates.

--%>

Title

测试成功!

3.3、添加实体类

public class User {//使用控件名和对象的属性名一致的方式进行接收,即username和前端的表单中的name值一致

private String username;

private String password;

private String[] box; //list集合接收也可以

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String[] getBox() {

return box;

}

public void setBox(String[] box) {

this.box = box;

}

@Override

public String toString() {

return "User{" +

"username='" + username + '\'' +

", password='" + password + '\'' +

", box=" + Arrays.toString(box) +

'}';

}

}

3.4、添加Controller类

@Controller

public class TestController {

//(3)使用控件名和对象的属性名一致的方式进行接收

@RequestMapping("/user")

public String test(User user){

System.out.println(user);

return "success";

}

}

3.5、启动tomcat进行测试

80c97d2f3e90

测试结果

4、随笔测试

4.1、index.jsp和success.jsp

Hello World!

Created by IntelliJ IDEA.

User: Mr Wei

Date: 2020/*/*

Time: 15:19

To change this template use File | Settings | File Templates.

--%>

Title

测试成功!

4.2、Controller类

package com.fan.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller

public class Controller1 {

@RequestMapping("/user")

public String test3(String username, String password, Date day){

System.out.println(username+"--"+password+"--"+day);

return "success";

}

// @InitBinder

// public void initBinder(WebDataBinder binder, WebRequest request) {

// //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd

// DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

// binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器

// }

}

4.3、启动tomcat进行测试

80c97d2f3e90

图片1.png

若输入的日期格式如:yyyy-MM-dd,则会报错。

80c97d2f3e90

图片2.png

若输入的日期格式如:yyyy/MM/dd,则会跳转成功,Spring MVC框架默认支持转换的日期格式是yyyy/MM/dd。

解决日期问题方式:

(1)使用string接受日期,接受后,再转换: SimpleDataFormate

(2)使用工具类处理日期(见有道云笔记SpringIOC1)

特别注意@RequestParam()

80c97d2f3e90

图片

注意:前端页面定义的是age,后端controller接收值是userage,@RequestParam()可以将这两个值对应起来,若age没有传值,则默认该userage为19.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值