0并且有问题在哪里放置我试过的公共文件在spring-mvc应用程序中放置图像/ CSS的位置?这个解决方案,但它不适合我
我试图将我的公共文件夹放在WEB-INF目录外的所有位置,但仍然没有
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<display-name>s-mvc</display-name>
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<mvc:resources mapping="/css/**" location="/css/"/>
</web-app>
Run Code Online (Sandbox Code Playgroud)
frontcontroller-servlet.xml中
<mvc:annotation-driven/>
<context:component-scan base-package="pl.skowronline.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
以及我如何调用css文件
<link type="text/css" href="/css/bootstrap.css" rel="stylesheet" />
JB Nizet的答案是正确的.但是,似乎您的应用程序的问题不仅仅是放置css/js文件的地方.对于Spring MVC,您必须正确配置所有配置,否则它将无法正常工作.
为简单起见,只需将css文件夹放在WEB-INF文件夹(/ WEB-INF/css)中,这样就可以在页面中像这样访问它:
<a href="<c:url value='/css/bootstrap.css'/>"
Run Code Online (Sandbox Code Playgroud)
该链接应该直接带您到css文件.如果有效,您可以将<a>标记更改<link>为CSS样式的标记.
如果它不起作用,有几件事要检查:
1)禁止访问文件的Spring Security约束
2)可能阻碍文件访问的各种过滤器/拦截器的影响
3)web.xml中的Servlet配置.确保没有调度程序拦截您对CSS文件的访问权限.
通常,<mvc:resources>会为您做上述所有事情.但万一它失败了,你可能想看一看.
对于<mvc:resources>错误:
匹配的通配符是严格的,但是没有为元素'mvc:resources'找到声明.
看起来您尚未声明正确的架构.例如,您应该在文件的开头添加以下行:
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http:/ /www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0 .xsd ">
更新:
作为您的回答,问题似乎在这里:
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
您应该将其更改为:
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/*.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
这将保存css/images文件的URL与控制器(您的servlet)的URL映射冲突,并让它让mvc:资源变得神奇.当然,您可以使用"html"部分所需的扩展名.对于一个漂亮的URL,我们可能会使用像土耳其URL重写这样的库来解决问题
小智 9
我觉得你和这个web.xml和那个混淆了application-context.xml.
本web.xml应包含webappXSD声明是这样,而对于调度的Servlet的映射.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>s-mvc</display-name>
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
其中application-context.xml包含spring-frameworkxsd声明如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:annotation-driven />
<context:component-scan base-package="pl.skowronline.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
文档的以下部分对此进行了描述。按照此处给出的说明进行操作,然后更改您的href: means: 在服务器根目录下的/css/bootstrap.css文件夹中。因此,除非您的应用程序部署为根应用程序,否则它将无法工作,因为您需要预先考虑应用程序的上下文路径:css
href="<c:url value='/css/bootstrap.css'/>"
Run Code Online (Sandbox Code Playgroud)
这意味着:在 css 文件夹中,就在webapp 的根目录下。如果您的 web 应用程序的上下文路径是/myFirstWebApp,则生成的 href 将是
href="/myFirstWebApp/css/bootstrap.css"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36220 次 |
| 最近记录: |