SpringMVC参数传递问题

    科技2026-04-21  3

    package com.zj.controller; //import java.util.HashMap; //import java.util.Map; import org.springframework.stereotype.Controller; //import org.springframework.ui.ModelMap; //import org.springframework.web.bind.annotation.ModelAttribute; //import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.RequestMapping; //import org.springframework.web.bind.annotation.RequestParam; //import org.springframework.web.servlet.ModelAndView; //import com.zj.entity.User; @Controller @RequestMapping("test") public class TestParam { //RedirectView重定向 @RequestMapping("/testParam1.do") public ModelAndView testParam() { RedirectView view = new RedirectView("testParam.do"); return new ModelAndView(view); } //返回String @RequestMapping("/testParam.do") public String testParam() { return "error"; return "redirct:testParamX.do"; } //ModelAttribute绑定数据 @RequestMapping("/testParam.do") public ModelAndView testParam(@ModelAttribute("username")String username) { System.out.println(username); return new ModelAndView("hello"); } //ModelMap传送数据 @RequestMapping("/testParam.do") public ModelAndView testParam(ModelMap map) { map.addAttribute("success",false); map.addAttribute("message","操作失败"); return new ModelAndView("hello"); } //ModelAndView传输数据 @RequestMapping("/testParam.do") public ModelAndView testParam() { Map<String, Object> data=new HashMap<>(); data.put("success",true); data.put("message", "操作成功"); return new ModelAndView("hello",data); } //对象接受 @RequestMapping("/testParam.do") public ModelAndView testParam(User user) { System.out.println(user.getUsername()+" "+user.getPassword()); return new ModelAndView("hello"); } //spring接受 @RequestMapping("testParam.do") public ModelAndView testParam(@RequestParam("username")String name,@RequestParam("password")String pwd) { System.out.println(name+" "+pwd); return new ModelAndView("hello"); } //Request对象接受 @RequestMapping("testParam.do") public ModelAndView testParam( HttpServletRequest request) { try { request.setCharacterEncoding("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username+" "+password); return new ModelAndView("hello"); } } <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>表单</h1> <form action="${pageContext.request.contextPath }/test/testParam.do" method="post"> 账号:<input type="text" name="username"/> 密码:<input type="password" name="password"/> <input type="submit" value="提交"/> </form> </body> </html> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>2020,您好</h1> <h1>是否成功:${success}</h1> <h1>提示信息:${message}</h1> <h1>姓名:${username }</h1> </body> </html>
    Processed: 0.009, SQL: 9