在RequestMapping上添加PathVariable更改视图路径 [英] Adding PathVariable changes view path on RequestMapping

查看:243
本文介绍了在RequestMapping上添加PathVariable更改视图路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图解析器:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}

和一个控制器:

@Controller
public class WorkflowListController {

 @RequestMapping(path = "/workflowlist", method = RequestMethod.GET)
 public ModelAndView index() throws LoginFailureException, PacketException,
        NetworkException {

    String profile = "dev";
    List<WorkflowInformation> workflows = workflows(profile);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("profile", profile);
    map.put("workflows", workflows);
    return new ModelAndView("workflowlist", map);
 }
}

,当我调用页面http://127.0.0.1:8090/workflowlist时,它会提供src/main/webapp/WEB-INF/jsp/workflowlist.jsp中的jsp.一切似乎都很好.

and when I call the page http://127.0.0.1:8090/workflowlist it serves the jsp from src/main/webapp/WEB-INF/jsp/workflowlist.jsp. That all seems to work well.

但是,当我尝试添加@PathVariable时:

However when I try to add a @PathVariable:

@RequestMapping(path = "/workflowlist/{profile}", method = RequestMethod.GET)
public ModelAndView workflowlist(@PathVariable String profile)
        throws LoginFailureException, PacketException, NetworkException {

    List<WorkflowInformation> workflows = workflows(profile);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("profile", profile);
    map.put("workflows", workflows);
    return new ModelAndView("workflowlist", map);
}

当我呼叫页面http://127.0.0.1:8090/workflowlist/dev时,会显示以下消息:

When I call the page http://127.0.0.1:8090/workflowlist/dev gives the following message:

There was an unexpected error (type=Not Found, status=404).
/workflowlist/WEB-INF/jsp/workflowlist.jsp

有人可以解释为什么我在两种情况下都返回相同的视图名称,但是在第二个示例中,它的行为有所不同吗?

Can someone explain why I'm returning the same view name in both cases but in the second example it is behaving differently?

我如何使它工作?

推荐答案

问题出在我的viewResolver上:

resolver.setPrefix("WEB-INF/jsp/");

应该是:

resolver.setPrefix("/WEB-INF/jsp/");

前面带有/的路径是从根目录(webapps文件夹)获取的,但是当/丢失时,它将成为相对路径.

With a / at the front the path is taken from the root (webapps folder) but when the / is missing it becomes a relative path.

关于视图解析器为什么只将目录的一部分作为路径的一部分,我从未得到任何答案,但这似乎是发生了. 因此,您可以定义具有不同根的视图子树.

I never got an answer as to why the view resolver only took the directory part of the path but that's what appeared to happen. It's probably so you can define subtrees of views with different roots.

这篇关于在RequestMapping上添加PathVariable更改视图路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆