Spring MVC系列:(10)结果的转发和重定向

时间:2022-12-11 07:43:27



    @RequestMapping(value="/delete")    public String delete(int id) throws Exception{
        System.out.println("删除用户->" + id);
        //转发到find(int)
        //return "forward:/user/find.action";
        //重定向到find(int)
        return "redirect:/user/find.action?id=4";
    }
    
    @RequestMapping(value="/find")
    public String find(int id) throws Exception{
        System.out.println("查找用户->" + id);
        return "/success.jsp";
    }


完整的UserAction.java

package com.rk.action;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.stereotype.Controller;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping(value="/user")public class UserAction {    @InitBinder    private void initBinder(ServletRequestDataBinder binder){        binder.registerCustomEditor(                Date.class,                 new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));    }        @RequestMapping(value="/delete")    public String delete(int id) throws Exception{        System.out.println("删除用户->" + id);        //转发到find(int)        //return "forward:/user/find.action";        //重定向到find(int)        return "redirect:/user/find.action?id=4";    }        @RequestMapping(value="/find")    public String find(int id) throws Exception{        System.out.println("查找用户->" + id);        return "/success.jsp";    } }