SpringMVC传值、转发、重定向例子

  1. 练习接收页面参数值
    1. 使用request
    2. 使用@RequestParam注解
    3. 使用实体对象
  2. 练习向页面传出数据
    1. 使用HttpServletRequest和session
    2. 使用ModelAndView对象  (内部为利用HttpServletRequest的Attribute传递数据到页面)
    3. 使用ModelMap对象 (内部为利用HttpServletRequest的Attribute传递数据到页面)
    4. 使用@ModelAttribute注解 (内部为利用HttpServletRequest的Attribute传递数据到页面)
  3. 练习使用session
    1. 在Controller方法参数上直接声明HttpSession即可使用
  4. 练习重定向
    1. 使用RedirectView
    2. 使用redirect:
复制代码
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(</span>"hello.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView hello(HttpServletRequest request,
        HttpServletResponse response) </span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception{   
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 测试request接收参数</span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test1.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test1(HttpServletRequest req){
    String userName </span>= req.getParameter("userName"<span style="color: #000000;">);
    String password </span>= req.getParameter("password"<span style="color: #000000;">);
    System.out.println(userName);
    System.out.println(password);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}<br></span></pre>

    /**
    * 测试sping会自动将表单参数注入到方法参数
    * 最好每个形参前都添加@requestparameter
    * 通过反射只能得到方法参数类型不能等到方法参数名称 没有加注解能成功获得为编译器自动添加
    */

    @RequestMapping("test2.do")
    public ModelAndView test2(String userName,
            @RequestParam("password") String pwd){
        System.out.println(userName+","+pwd);
        return new ModelAndView("jsp/hello");
    }
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 测试对象接收参数
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test3.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test3(User user){
    System.out.println(user);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 使用ModelAndView传出参数 内部 HttpServletRequest的Attribute传递到jsp页面<br>   * ModelAndView(String viewName,Map data)data是处理结果
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test4.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test4(User user){
    Map</span>&lt;String, Object&gt; data = <span style="color: #0000ff;">new</span> HashMap&lt;String, Object&gt;<span style="color: #000000;">();
    data.put(</span>"user"<span style="color: #000000;">, user);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">,data);
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 使用ModelMap传出参数   内部HttpServletRequest的Attribute传递到jsp页面
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test5.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test5(User user,ModelMap modelMap){
    modelMap.put(</span>"user"<span style="color: #000000;">, user);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 使用ModelAttribute 内部HttpServletRequest的Attribute传递到jsp页面
 * 在Contoller的参数部分或者bean属性方法上使用
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test6.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> ModelAndView test6(@ModelAttribute("user"<span style="color: #000000;">)User user){
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}

@ModelAttribute(</span>"age"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Integer getAge(){
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age;
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * session存储   可以使用HttpServletRequest的getSession方法访问
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test7.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test7(HttpServletRequest req){
    HttpSession session </span>=<span style="color: #000000;"> req.getSession();
    session.setAttribute(</span>"salary", 6000.0<span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello"<span style="color: #000000;">);
}

</span><span style="color: #008000;">//</span><span style="color: #008000;">返回String 转发</span>
@RequestMapping("/test8.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String test8(User user, ModelMap model) {
    model.addAttribute(</span>"user"<span style="color: #000000;">, user);
    </span><span style="color: #0000ff;">return</span> "jsp/hello"<span style="color: #000000;">;
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 错误页面
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test9.do"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String test9(){
    </span><span style="color: #0000ff;">return</span> "error/error"<span style="color: #000000;">;
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 *使用RedirectView重定向
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test10"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ModelAndView test10(User user){
    </span><span style="color: #0000ff;">if</span>(user.getUserName().equals("123"<span style="color: #000000;">)){
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView("jsp/hello");<span style="color: #008000;">//</span><span style="color: #008000;">test10.do 转发</span>
    }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ModelAndView(<span style="color: #0000ff;">new</span> RedirectView("test9.do"));<span style="color: #008000;">//</span><span style="color: #008000;">test9.do?age=22 重定向</span>

}
}

</span><span style="color: #008000;">/**</span><span style="color: #008000;">
 * 使用redirect重定向
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
@RequestMapping(</span>"test11"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String test11(User user){
    </span><span style="color: #0000ff;">if</span>(user.getUserName().equals("123"<span style="color: #000000;">)){
        </span><span style="color: #0000ff;">return</span> "jsp/hello"<span style="color: #000000;">;
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">return</span> "redirect:test9.do"<span style="color: #000000;">;
    }
}

}

复制代码

user实体

复制代码
package 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;
    }
}
复制代码
原文地址:https://www.cnblogs.com/jpfss/p/9542031.html