【问题标题】:Spring boot multiple controllers and extending the urlSpring引导多个控制器并扩展url
【发布时间】:2018-02-19 16:53:32
【问题描述】:

这是控制器 1:

@Controller
@RequestMapping("/clusters")
public class ClusterController {

    @Autowired
    private ClusterService clusterService;

    @RequestMapping(method = RequestMethod.GET)
    public String getAllClusters(Model model){

        List<Cluster> clusters = this.clusterService.getAllClusters();
        model.addAttribute("clusters", clusters);

        return "clusters";
    }

    @RequestMapping(value = "/clusterInfo", method = RequestMethod.GET)
    public String getCluster(@RequestParam("id") Integer id, Model model){
        Cluster cluster = this.clusterService.findClusterById(id);
        model.addAttribute("cluster", cluster);

        return "testcluster";
    }

控制器 2:

@Controller
@RequestMapping("/requirements")
public class RequirementController {


    @RequestMapping("/")
    public String getRequirements(){

        return "requirements";
    }
}

首先我看到所有的集群。之后,我可以单击站点上的按钮来查看单个集群(来自控制器 1 的方法 2)。

当我看到一个单独的集群时,我想点击一个按钮来查看链接到该集群的需求。其方法在 Controller 2 中。

从控制器 1 到 2 时,我希望有以下 url 路径。

http://localhost:8080/clusters

http://localhost:8080/clusters/clusterInfo?id=1

http://localhost:8080/clusters/clusterInfo?id=1/requirements

这是我用来从 url 2 转到 3 的按钮。 更多

它不起作用并给我一个错误。我的问题是,如何使用指定的 url 路径从控制器 1 转到控制器 2 并检索控制器 2 中的 id=1?

这是我的第一个项目,所以我对 Spring Boot 和 Thymeleaf 了解不多。

【问题讨论】:

    标签: spring thymeleaf


    【解决方案1】:

    ? 标记查询字符串参数的开始,根据 URI RFC 标准,您不能在查询字符串参数之后指定路径。 我建议改为将 URI 更改为:

    http://localhost:8080/clusters/<clusterId>/clusterInfo
    Ex: http://localhost:8080/clusters/1/clusterInfo
    

    http://localhost:8080/clusters/<clusterId>/requeriments
    Ex: http://localhost:8080/clusters/1/requirements
    

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多