Recently, I used the springmvc spring mybatis framework to write a program, the request was successful and the data was obtained, but the css style could not be loaded, but the path was correct, the css file encoding was also utf-8, and the Firefox debug always showed that the request was not received (I almost suspect that I wrote the path myself There is a problem), and finally got it today. Found three solutions, but one I don't know why doesn't work.
Option 1 (personally thinks the most convenient): configure the following code in web.xml
Generally, the default servlet name of a web application server is "default", so here we activate Tomcat's defaultServlet to process static files
<!-- 配置静态资源 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
If the default servlet name for all your web application servers is not "default", you need to explicitly specify it via the default-servlet-name attribute:
< mvc:default-servlet-handler default-servlet-name= "The default servlet name used by the web server" />
Option two :
<!-- springmvc-servlet.xml控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 一启动就要加载配置文件 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Note: The request format of the browser at this time is as follows:
http://localhost:8080/WorkOrder/login.action
have .action suffix
My original way of writing:
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
When the interception method is / All requests are intercepted, and the css file is a static resource and cannot be accessed
Option three :
<mvc:resources mapping="/css/**" location="/static/css/"/>
I don't know why the configuration is unsuccessful in this way. If you know it, I hope to share it *_*
Reprinted from: http://blog.csdn.net/tonytfjing/article/details/39207551
Summary: When solving problems, a good question can do more with less, and the more it can show the essence of the problem, the more efficient it can be.
4 did not try
Or a workaround: add the following line to the spring configuration file:
<mvc:default-servlet-handler/>
Note that it needs to be spring 3.0.5 or later
最近用springmvc spring mybatis框架写程序,请求成功并获得数据,唯独css样式不能加载,但路径正确,css文件编码也是utf-8,用火狐debug总是显示未请求到(都快怀疑自己写路径有问题了),今天终于搞定了。发现三种解决方式,但有一个我不知道什么原因用不了。
方案一(个人认为最方便的):在web.xml里配置如下代码即可
一般Web应用服务器默认的Servlet名称是"default",所以这里我们激活Tomcat的defaultServlet来处理静态文件
<!-- 配置静态资源 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
<url-pattern>/css/*</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
如果你所有的Web应用服务器的默认Servlet名称不是"default",则需要通过default-servlet-name属性显示指定:
<mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" />
方案二:
<!-- springmvc-servlet.xml控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 一启动就要加载配置文件 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Note: The request format of the browser at this time is as follows:
http://localhost:8080/WorkOrder/login.action
have .action suffix
My original way of writing:
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
When the interception method is / All requests are intercepted, and the css file is a static resource and cannot be accessed
Option three :
<mvc:resources mapping="/css/**" location="/static/css/"/>
I don't know why the configuration is unsuccessful in this way. If you know it, I hope to share it *_*
Reprinted from: http://blog.csdn.net/tonytfjing/article/details/39207551
Summary: When solving problems, a good question can do more with less, and the more it can show the essence of the problem, the more efficient it can be.
4 did not try
Or a workaround: add the following line to the spring configuration file:
<mvc:default-servlet-handler/>
Note that it needs to be spring 3.0.5 or later