本文主要是介绍springMVC 传递参数和回传数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、基本数据类型+String上传:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a><form action="<%=path %>/user/login.do" method="post">账号:<input type="text" name="account"><br>密码:<input type="password" name="pwd"><br>账号:<input type="submit" value="提交"><br></form>
</body>
</html>
Controller写法:
package com.xingxue.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import com.xingxue.service.UserService;@Controller
@RequestMapping("/user")
public class UserContrller {@Autowiredpublic UserService service;public ModelAndView userLogin1(String account, String pwd) {System.out.println("canshu : " + account);System.out.println("canshu : " + pwd);ModelAndView mv = new ModelAndView();mv.setViewName("user/ok");mv.addObject("msg", "ok!!!!!!");return mv;}}
测试通过
复杂数据类型,实体对象作为参数传递:
@RequestMapping("/login2.do")public ModelAndView userLogin2(User user) {System.out.println("canshu : " + user.getAccount());System.out.println("canshu : " + user.getPwd());ModelAndView mv = new ModelAndView();mv.setViewName("user/ok");mv.addObject("msg", "ok!!!!!!");return mv;}
页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a><form action="<%=path %>/user/login2.do" method="post">账号:<input type="text" name="account"><br>密码:<input type="password" name="pwd"><br>账号:<input type="submit" value="提交"><br></form>
</body>
</html>
此处注意:区别与struts2, struts2 提交参数是对象名.属性名 ,springMVC是直接属性名,springMVC会自动到该对象获取该属性并且注入数据。
HashMap接收参数
此处需要注意:@RequestParam注解,表示吧参数全部注入到map对象里面
@RequestMapping("/login3.do")public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param) {System.out.println(param.get("account"));System.out.println(param.get("pwd"));ModelAndView mv = new ModelAndView();mv.setViewName("user/ok");mv.addObject("msg", "ok!!!!!!");return mv;}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><html>
<body>
<h2>Hello World!</h2>
<%
String path = request.getContextPath();
%>
<a href="<%=request.getContextPath()%>/user/delete.do?id=10015"> delete </a><form action="<%=path %>/user/login3.do" method="post">账号:<input type="text" name="account"><br>密码:<input type="password" name="pwd"><br>账号:<input type="submit" value="提交"><br></form>
</body>
</html>
springMVC 获取servletAPI对象
@RequestMapping("/login3.do")public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param,HttpSession session,HttpServletRequest request,HttpServletResponse response) {ModelAndView mv = new ModelAndView();HashMap<String, Object> loginUser = this.service.userLogin(param);if(loginUser == null) {mv.addObject("msg", "NO!!!!!!");mv.setViewName("index");} else {session.setAttribute("loginUser", loginUser);mv.addObject("msg", "OK!!!!!!");mv.setViewName("user/ok");}return mv;}
springMVC如果需要获取servletAPI ,只需要在方法的参数里面定义该类型的参数,springMVC就会自动注入一个该类型对象给我们。
参数回传:
ModelAndView mv = new ModelAndView();HashMap<String, Object> loginUser = this.service.userLogin(param);if(loginUser == null) {mv.addObject("msg", "NO!!!!!!");mv.setViewName("../index");} else {session.setAttribute("loginUser", loginUser);mv.addObject("msg", "OK!!!!!!");mv.setViewName("user/ok");}
springMVC提供了一个modelAndView的类,这个对象封装了我们的视图信息和回传数据信息,想要回传数据,直接利用该对象的addObejct存入即可,这样我们的数据会存入request对象,然后跳转页面,所以页面直接从request获取即可
这篇关于springMVC 传递参数和回传数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!