简体   繁体   English

Spring 4-MVC-URL参数和静态资源路径

[英]Spring 4 - MVC - URL param and static resource path

I have a spring mvc application, in a page I list the "Group" details in a table, fetched from database. 我有一个spring mvc应用程序,在页面中,我在表中列出了从数据库中获取的“组”详细信息。 The url of every group is set to "/viewgroup/410", where 410 is the groupid which will be loaded from the database and displayed in the viewgroup.jsp page. 每个组的url设置为“ / viewgroup / 410”,其中410是将从数据库中加载并显示在viewgroup.jsp页面中的groupid。

<td><a href="viewgroup/${group.groupid}">${group.name}</a></td>

So, the controller method has the 因此,控制器方法具有

@RequestMapping("/viewgroup/{groupid}") 
public String viewGroup() {
    ...
}

like this. 像这样。 The jsp pages could not load the static resources which I have in the directory structure JSP页面无法加载目录结构中的静态资源

+webapp
  +resources
     +css
     +js
     +images
  +views
    +login.jsp

The jsp pages has the below path for image/js/css. jsp页面具有image / js / css的以下路径。

<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">

I tried adding 我尝试添加

@Controller
@RequestMapping("/viewgroup/**")
public class ViewGroupController {
   ...
}

and this in jsp 这在jsp中

<link href="viewgroup/resources/css/bootstrap.css" rel="stylesheet" media="screen">

Still the jsp page loads could not load the static resources. 仍然,jsp页面加载无法加载静态资源。 How do I provide the resource path of the static resources in jsp pages when I pass params in the url? 当我在url中传递参数时,如何在jsp页面中提供静态资源的资源路径?

The fact that you pass query parameters through any URL of your project does not affect the way it addresses static resources. 您通过项目的任何URL传递查询参数的事实并不影响其处理静态资源的方式。 You need just to include a link to your static resources (as a relative path for example) as follow : 您只需要包括指向静态资源的链接(例如,作为相对路径),如下所示:

<link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">

The resolution of such static resources is completely independent of the page you're currently looking at, be it /viewgroup/410 or /foo/bar . 此类静态资源的分辨率完全独立于您当前正在查看的页面,无论是/viewgroup/410还是/foo/bar That's the reason why you don't need the "viewgroup" at the beginning of your link's href. 这就是为什么链接的href开头不需要“视图组”的原因。

But you do need to tell Spring MVC how it should address such static resources. 但是您确实需要告诉Spring MVC它应该如何处理此类静态资源。 Usually, this is done in the Spring configuration. 通常,这是在Spring配置中完成的。 For example, if you use Spring MVC java config through an WebMvcConfigurer instance, you should override the addResourceHandlers method in such way : 例如,如果通过WebMvcConfigurer实例使用Spring MVC java config,则应以以下方式覆盖addResourceHandlers方法:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }


EDIT : My bad, I thought Spring MVC + any view technology (JSP, Thymeleaf, etc etc) would automatically resolve link's href against the root path of the web-app but this is obviously not true to raw HTML which relative link's href are resolved against current path. 编辑:我不好,我认为Spring MVC +任何视图技术(JSP,Thymeleaf等)都会根据Web应用程序的根路径自动解析链接的href,但这显然不适用于解析相对链接的href的原始HTML反对当前的道路。 So in the end, as found by OP, links are only to be resolved against root path if they are processed with the view technology in use (in the example with JSP : <c:url value="/resources/css/bootstrap.css"/> ) 因此,最后,如OP所见,只有使用正在使用的视图技术处理链接时,才可以针对根路径解析链接(在使用JSP的示例中: <c:url value="/resources/css/bootstrap.css"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM