SpringMVC传值、转发、重定向例子
2021-07-13 16:07
标签:spring 图片 ati port ota list 注入 访问 map对象 /** user实体 SpringMVC传值、转发、重定向例子 标签:spring 图片 ati port ota list 注入 访问 map对象 原文地址:https://www.cnblogs.com/jpfss/p/9542031.html
package web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import entity.User;
//非注解方式
//public class HelloController implements Controller {
//
//
// public ModelAndView handleRequest(HttpServletRequest request,
// HttpServletResponse response) throws Exception {
// System.out.println("Hello, Controller.");
// return new ModelAndView("jsp/hello");
// }
//
//}
@Controller
@RequestMapping("/demo")
public class HelloController{
private Integer age=22;
@RequestMapping("hello.do")
public ModelAndView hello(HttpServletRequest request,
HttpServletResponse response) throws Exception{
return new ModelAndView("jsp/hello");
}
/**
* 测试request接收参数*/
@RequestMapping("test1.do")
public ModelAndView test1(HttpServletRequest req){
String userName = req.getParameter("userName");
String password = req.getParameter("password");
System.out.println(userName);
System.out.println(password);
return new ModelAndView("jsp/hello");
}
* 测试sping会自动将表单参数注入到方法参数
* 最好每个形参前都添加@requestparameter
* 通过反射只能得到方法参数类型不能等到方法参数名称 没有加注解能成功获得为编译器自动添加
*/ @RequestMapping("test2.do")
public ModelAndView test2(String userName,
@RequestParam("password") String pwd){
System.out.println(userName+","+pwd);
return new ModelAndView("jsp/hello");
}
/**
* 测试对象接收参数
*/
@RequestMapping("test3.do")
public ModelAndView test3(User user){
System.out.println(user);
return new ModelAndView("jsp/hello");
}
/**
* 使用ModelAndView传出参数 内部 HttpServletRequest的Attribute传递到jsp页面
* ModelAndView(String viewName,Map data)data是处理结果
*/
@RequestMapping("test4.do")
public ModelAndView test4(User user){
Mappackage com.tarena.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}