如何避免在 Spring MVC 中硬链接前端资源

发布于 2024-09-06 21:56:42 字数 1155 浏览 8 评论 0原文

我是 Spring MVC 的新手,有使用 PHP MVC 框架和 ROR 的经验。我很难找到适当的方法来组织前端资产并将其包含到视图模板中。

这是 Roo 为默认样式表生成的默认代码:

<spring:theme code="styleSheet" var="roo_css"/> 
<spring:url value="/${roo_css}" var="roo_css_url"/>  
<spring:url value="/static/images/favicon.ico" var="favicon" />
<link rel="stylesheet" type="text/css" media="screen" href="${roo_css_url}"></link>

这对我来说似乎完全没有必要。我们从 spring:theme 代码列表中调用一个变量。将其分配给视图范围中的变量,然后调用该视图变量

理想情况下,我希望有一些路径标记,例如: ${imagePage}、${stylePath} 等。我们可以插入并用于软链接。

希望有人能给我指出一些高质量的 SpringMVC 文档或给出一些示例。谢谢

更新:

我已经看到了一些示例,engfer 在下面发布了一个示例,建议在 html 中使用 spring 标签来输出 href,如下所示

<a href="<spring:url url='/about'/ />">About</a>

这是可以接受的,但是我从 jetty 收到以下错误

Caused by: org.apache.jasper.JasperException: /WEB-INF/views/footer.jspx(6,22) The value of attribute "href" associated with an element type "null" must not contain the '<' character.

我是否缺少编码的某些内容?还是DTD?

更新:

显然,内联 href 样式仅适用于 .jsp 文件,因为 .jspx (我使用的)是严格的 xml。 .jspx 相对于 .jsp 有哪些优点?

I am new to spring MVC coming with experience using PHP MVC frameworks and ROR. I am having a hard time finding the appropriate way to organize and include front end assets into the view templates.

Here is the default code Roo produces for the default style sheet:

<spring:theme code="styleSheet" var="roo_css"/> 
<spring:url value="/${roo_css}" var="roo_css_url"/>  
<spring:url value="/static/images/favicon.ico" var="favicon" />
<link rel="stylesheet" type="text/css" media="screen" href="${roo_css_url}"></link>

This seems totally unnecessary to me. We are calling a variable from the spring:theme code list. Assigning it to a variable in the view scope/ and then calling that view variable for the

Ideally I would like to have some path tokens like: ${imagePage}, ${stylePath}, etc. That we could drop in and use for soft linking.

Hopefully somebody can point me to some quality SpringMVC documentation or give some examples. Thanks

Update:

I have seen a few examples and engfer has posted one below that suggest using the spring tags within the html to ouput the href like so

<a href="<spring:url url='/about'/ />">About</a>

This would be acceptable however I am getting the following error from jetty

Caused by: org.apache.jasper.JasperException: /WEB-INF/views/footer.jspx(6,22) The value of attribute "href" associated with an element type "null" must not contain the '<' character.

Am I missing something with the encoding? or DTD?

Update:

So apparently the in-line href style only works with .jsp files as .jspx (What im using) is strict xml. What are the benefits of .jspx over .jsp and

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-09-13 21:56:42

您从 Roo 提供的代码有点不必要。如果您按照 tkeE2036 指出的那样查看 Spring MVC 文档...您将看到主题部分 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-themeresolver

<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>

Spring 主题标签使您能够将 CSS/图像/js(想象一个包含单词“Hello”的图像,需要将其更改为英语/西班牙语/等)国际化为单独的国际化主题文件,如下所示http://java.sun.com/javase /6/docs/api/java/util/ResourceBundle.html 约定,Spring 将自动解析正确的资源捆绑主题。


如果您仍然想要 ${imagePath} 或其他内容,您可以使用 标签来完成您的工作。

<spring:url value="/images" var="imagePath"/>
<link rel="stylesheet" href="${imagePath}/foo.png" type="text/css"/>

The code you have provided from Roo is a little unnecessary. If you look at the Spring MVC documentation as tkeE2036 pointed out... you will see the themes section http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-themeresolver

<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>

The Spring theme tag gives you the ability to internationalize your CSS/images/js (imagine an image that has the word 'Hello' which needs to be changed for English/Spanish/etc) into separate internationalized theme files that follow the http://java.sun.com/javase/6/docs/api/java/util/ResourceBundle.html convention and Spring will automatically resolve the correct resource bundled theme.


If you still want your ${imagePath} or whatever, you can use the <spring:url/> tag to get your job done.

<spring:url value="/images" var="imagePath"/>
<link rel="stylesheet" href="${imagePath}/foo.png" type="text/css"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文