你可以在Spring控制器中如何定位根路径?

huangapple go评论66阅读模式
英文:

How can I target the root path in Spring controller?

问题

我正在尝试在我的Spring控制器中添加默认路径。

例如:我希望http://localhost:8080渲染与http://localhost:8080/index相同的视图。

我尝试了以下内容:

@Controller
@RequestMapping("/")
public class RecipeController {

    @Autowired
    private RecipeService service;

    @RequestMapping // http://localhost:8080
    public String root(Model model) {
        List<Recipe> recipes = service.fetchAll();
        model.addAttribute("recipes", recipes);
        return "index";
    }

    @GetMapping(value = {"/index"}) // http://localhost:8080/index
    public String index(Model model) {
        List<Recipe> recipes = service.fetchAll();
        model.addAttribute("recipes", recipes);
        return "index";
    }

}

在http://localhost:8080/index上运行良好。但是,在http://localhost:8080上从未进入root函数。

所以我尝试了这个:

@Controller
public class RecipeController {

    @Autowired
    private RecipeService service;

    @RequestMapping("/") // http://localhost:8080
    public String root(Model model) {
        List<Recipe> recipes = service.fetchAll();
        model.addAttribute("recipes", recipes);
        return "index";
    }

    @GetMapping(value = {"/index"}) // http://localhost:8080/index
    public String index(Model model) {
        List<Recipe> recipes = service.fetchAll();
        model.addAttribute("recipes", recipes);
        return "index";
    }

}

它产生了相同的结果。

英文:

I am trying to add a default path in my Spring controller.
For example: I would like http://localhost:8080 to render the same view as http://localhost:8080/index

I tried that:

@Controller
@RequestMapping(&quot;/&quot;)
public class RecipeController {

    @Autowired
    private RecipeService service;

    @RequestMapping // http://localhost:8080
    public String root(Model model) {
        List&lt;Recipe&gt; recipes = service.fetchAll();
        model.addAttribute(&quot;recipes&quot;, recipes);
        return &quot;index&quot;;
    }

    @GetMapping(value = {&quot;/index&quot;}) // http://localhost:8080/index
    public String index(Model model) {
        List&lt;Recipe&gt; recipes = service.fetchAll();
        model.addAttribute(&quot;recipes&quot;, recipes);
        return &quot;index&quot;;
    }

}

It is working fine on http://localhost:8080/index.
However, it never enters the root function on http://localhost:8080.

So I tried that:

@Controller
public class RecipeController {

    @Autowired
    private RecipeService service;

    @RequestMapping(&quot;/&quot;) // http://localhost:8080
    public String root(Model model) {
        List&lt;Recipe&gt; recipes = service.fetchAll();
        model.addAttribute(&quot;recipes&quot;, recipes);
        return &quot;index&quot;;
    }

    @GetMapping(value = {&quot;/index&quot;}) // http://localhost:8080/index
    public String index(Model model) {
        List&lt;Recipe&gt; recipes = service.fetchAll();
        model.addAttribute(&quot;recipes&quot;, recipes);
        return &quot;index&quot;;
    }

}

It gives the same result.

答案1

得分: 1

你可以将它统一为以下形式:

@RequestMapping(value = {"/", "/index"})

在Spring中,注解 @RequestMapping 可以接受多个值,将不同的URL映射到控制器的同一个方法。

希望它能够正常工作!

英文:

You could unify this as follows:

@RequestMapping(value = {&quot;/&quot;, &quot;/index&quot;})

In Spring, the annotation @RequestMapping can receive multiple values to map different URLs to the same method of a controller.

Hope it works!

huangapple
  • 本文由 发表于 2023年5月8日 01:55:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定