Java 如何构建级联控制器?

Java 如何构建级联控制器?,java,spring-mvc,url-redirection,Java,Spring Mvc,Url Redirection,我需要像这样输入URLhttp://localhost:8080/first之后,我的控制器必须转到http://localhost:8080/second等等,直到我来到http://localhost:8080/end。它有点像递归。最后,我需要列出一个清单 @Controller @RequestMapping(value = "/", method = RequestMethod.GET) public class CascadeController { @Request

我需要像这样输入URL
http://localhost:8080/first
之后,我的控制器必须转到
http://localhost:8080/second
等等,直到我来到
http://localhost:8080/end
。它有点像递归。最后,我需要列出一个清单

@Controller
@RequestMapping(value = "/", method = RequestMethod.GET)
public class CascadeController {    
    @RequestMapping("/first")
    String first(ModelMap model) {
        model.put("list", new ArrayList<String>());
        return "/second";
    }

    @RequestMapping("/second")
    String second(ModelMap model) {
        ((List) model.get("list")).add("A");
        return "/third";
    }

    @RequestMapping("/third")
    String third(ModelMap model) {
        ((List) model.get("list")).add("B");
        return "end";
    }
}
@控制器
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共类级联控制器{
@请求映射(“/first”)
字符串优先(模型映射模型){
put(“list”,新的ArrayList());
返回“/秒”;
}
@请求映射(“/秒”)
字符串秒(ModelMap模型){
((列表)model.get(“列表”).add(“A”);
返回“/第三”;
}
@请求映射(“/third”)
第三个字符串(ModelMap模型){
((列表)model.get(“列表”).add(“B”);
返回“结束”;
}
}
end.jsp

<%@ page import="java.util.List" %>
<html>
<body>
   <%for(String s : (List<String>) request.getAttribute("list")){%>
      <%=s%>
   <%}%>
</body>
</html>


有人能解释我的代码有什么问题吗?

用户是否需要查看放置在模型上的/第一页或/第二页的数据? 如果没有,请尝试更改:

返回“/秒”
返回“重定向:/second”

return”/third
返回“重定向:/third”

如果用户需要在/first或/second页面上查看X次数据,那么从javascript执行重定向如何

编辑: 关于flash属性,请参见以下帖子:

@RequestMapping(“/first”)
字符串优先(ModelMap模型,最终重定向属性){
put(“list”,新的ArrayList());
redirectAttributes.addFlashAttribute(“list”,newArrayList());
返回“重定向:/second”;
}
@请求映射(“/秒”)
字符串秒(ModelMap模型,最终重定向属性){
List=(List)model.get(“List”);
列表。添加(“A”);
redirectAttributes.addFlashAttribute(“列表”,列表);
返回“重定向:/third”;
}
@请求映射(“/third”)
第三个字符串(ModelMap模型,最终重定向属性){
List=(List)model.get(“List”);
列表。添加(“B”);
redirectAttributes.addFlashAttribute(“列表”,列表);
返回“结束”;
}

我建议您看看。从项目说明中复制:

SpringWebFlow的最佳选择是具有 受控导航[…]这些场景的共同点是 以下一个或多个特征:

  • 有一个明确的起点和终点
  • 用户必须按特定顺序浏览一组屏幕
  • 直到最后一步才完成更改
  • 一旦完成,就不可能意外地重复事务

你得到了什么样的结果?它与你的期望有什么不同?你听说过SpringWebFlow吗?可能有用..我不能使用Spring WF,只能使用MVC。>>用户是否需要查看放置在模型上的/first或/second页面的数据?是的,我需要保存一个模型。
  @RequestMapping("/first")
    String first(ModelMap model, final RedirectAttributes redirectAttributes) {
        model.put("list", new ArrayList<String>());
        redirectAttributes.addFlashAttribute("list", new ArrayList<String>());
        return "redirect:/second";
    }

    @RequestMapping("/second")
    String second(ModelMap model, final RedirectAttributes redirectAttributes) {
        List list = (List) model.get("list");
        list.add("A");
        redirectAttributes.addFlashAttribute("list", list);
        return "redirect:/third";
    }

    @RequestMapping("/third")
    String third(ModelMap model, final RedirectAttributes redirectAttributes) {
        List list = (List) model.get("list");
        list.add("B");
        redirectAttributes.addFlashAttribute("list", list);
        return "end";
    }