相关疑难解决方法(0)

Tomcat在Spring MVC应用程序上提供静态资源

我正在构建一个Spring MVC应用程序,并且frontController servlet映射为"/"拦截所有请求,我将能够从tomcat提供静态内容(.js,.css,.png ...)不是春天.我的app结构是

-webapp/
   styles/
   images/
   WEB-INF/
          views/
Run Code Online (Sandbox Code Playgroud)

默认情况下,因为frontController映射到我的应用程序的上下文根,它处理所有请求但不提供任何静态资源.下面是静态资源的mvc配置.

<mvc:resources mapping="/resources/**" location="/"/>
Run Code Online (Sandbox Code Playgroud)

页面的代码是:

<img src="resources/images/logo.png" />
Run Code Online (Sandbox Code Playgroud)

我需要配置Tomcat来提供静态资源而不需要Spring交互.

有什么建议吗?

spring tomcat static-resource

5
推荐指数
2
解决办法
9703
查看次数

为什么在 Spring 中有两种处理静态资源的方法(addResourceHandlers 和容器的默认 Servlet”)?

我是春天的新手。我注意到在处理静态资源时,有两个选项可用:


选项1:

如果使用以下代码DispatcherServlet将Spring映射到/,这使其成为“默认 Servlet”,则可以使用RequestMapping注释(覆盖AbstractAnnotationConfigDispatcherServletInitializer类)将某些静态资源映射到 Spring 处理程序:

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}
Run Code Online (Sandbox Code Playgroud)

然后我们仍然可以启用容器的“默认 Servlet”来处理那些 URL 模式没有被 Spring 请求映射(覆盖WebMvcConfigurerAdapter类)覆盖的静态资源:

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
Run Code Online (Sandbox Code Playgroud)

这基本上使用servlet容器的“默认servlet”为包罗万象的处理所有的静态资源错过由Spring的DispatcherServlet


选项 2:

(覆盖WebMvcConfigurerAdapter类)

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    registry.addResourceHandler("*.efi").addResourceLocations("/");
}
Run Code Online (Sandbox Code Playgroud)
  • 为什么有两种选择?
  • 这些方法之间的主要区别是什么?
  • 还有其他选择吗?

我通常选择选项 2,因为我想坚持使用 Spring,但我知道这不是一个强有力的理由。


一些与静态资源处理相关的参考:

java configuration spring spring-mvc

5
推荐指数
1
解决办法
2268
查看次数

Spring MVC 和错误 400:SRVE0295E

我对简单的 MVC 应用程序有问题。

dayoff.html
<div id="dayoff_operations" class="big_box_1">
    <form:form modelAttribute="dayOff" enctype="multipart/form-data" action="/dayoff.html" >
        <div style="width: 50%; float: left; margin-top: 0px ">
            <form:label path="forDate" ><spring:message code="dayoff.fordate.label"/></form:label>
            <form:input path="ax_forDate" id="datepicker" style="width:65px" readonly="readonly"/>&nbsp;&nbsp;
        </div>
        <div style="width: 50%; float: right; margin-top: 0px ">
            <form:label path="toDate" ><spring:message code="dayoff.todate.label"/></form:label>
            <form:input path="ax_toDate" id="datepicker2" style="width:65px" readonly="readonly" />&nbsp;&nbsp;
        </div>
        <div style="float: left">
            <form:select cssClass="more" path="dayofftype" items="${dayoffTypes.list}" itemLabel="label" itemValue="value" htmlEscape="false" cssErrorClass="errorsField"/>
            <form:errors path="dayofftype" cssClass="errors"/>
        </div>
        <input type="submit" name="add" value="Add"/>
    </form:form>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@Controller
@RequestMapping("/dayoff")
@SessionAttributes(types = {DayOff.class})
public class DaysOffController {
private …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

1
推荐指数
1
解决办法
1万
查看次数

Spring MVC 404找不到错误

我试图在Spring MVC中运行一个项目。这是代码

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <h1>Spring 3 Register!</h1>
        <a href="register.htm">click</a>
        <form:form action="${pageContext.request.contextPath}/register.htm" method="POST" modelAttribute="userForm">
            <table>
                <tr>
                    <td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="username" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:password path="password" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><form:input path="email" /></td>
                </tr>
                <tr>
                    <td>BirthDate (mm/dd/yyyy)</td>
                    <td><form:input path="birthDate" /></td>
                </tr>
                <tr>
                    <td>Profession</td> …
Run Code Online (Sandbox Code Playgroud)

java spring jsp spring-mvc

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

spring ×4

java ×3

spring-mvc ×3

configuration ×1

jsp ×1

static-resource ×1

tomcat ×1