【问题标题】:Added "mvc:resources" tag to dispatcher-servlet.xml to add js and css, it is giving HTTP Status 404向 dispatcher-servlet.xml 添加“mvc:resources”标签以添加 js 和 css,它给出 HTTP 状态 404
【发布时间】:2016-07-29 12:15:00
【问题描述】:

我正在开发一个 spring-MVC 应用程序。它工作正常,但是当我向 dispatcher-servlet.xml 添加“mvc:resources”标签以添加 js 和 css 时,它给出了 HTTP 状态 404。我的 dispatcher-servlet.xml如下:

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:annotation-config />
    <context:component-scan base-package="com.asurion" />

    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        p:url="jdbc:sqlserver://localhost:1433;DataBaseName=test" p:username="test"
        p:password="test" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.asurion.dialect.SQlServerDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="reportDAO" class="com.asurion.dao.ReportDaoImpl"></bean>
    <bean id="reportManager" class="com.asurion.service.ReportManagerImpl"></bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

而我的 web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             metadata-complete="true">
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>list</welcome-file>
  </welcome-file-list>
</web-app>

控制器代码为:

package com.asurion.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asurion.entity.ReportAttr;
import com.asurion.service.ReportManager;

@Controller
public class ReportQueryController {

    @Autowired
    private ReportManager reportManager;

    @RequestMapping(value = "/searchSubmit", method = RequestMethod.POST)
    public String searchSubmit(@ModelAttribute("attributes") ReportAttr attributes, BindingResult result,
            HttpServletRequest request, HttpServletResponse response, Model model) {

        return reportManager.loadReportJson(request, attributes, model);
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String searchParams(HttpServletRequest request, Model model) {

        ReportAttr attributes = new ReportAttr();
        model.addAttribute("attributes", attributes);
        return "search";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String listReport(Model map) {
        map.addAttribute("lists", reportManager.getAllRecords());

        return "list";
    }

}

search.jsp 是:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Report Parameters</title>
<link type="text/css" rel="stylesheet" href="<c:url value="css/components.css" />" />
<link type="text/css" rel="stylesheet" href="<c:url value="css/responsee.css" />" />
<link type="text/css" rel="stylesheet" href="<c:url value="css/template-style.css" />" />

<script src="<c:url value="js/jquery-1.12.3.js" />"></script>
<script src="<c:url value="js/jquery-ui.min.js" />"></script>
<script src="<c:url value="js/responsee.js" />"></script>
<script src="<c:url value="js/modernizr.js" />"></script>

<script type="text/javascript">
    $(function() {
        $('#btnSubmit').click(function() {
            $('#status').val("submit");
        });
    });
</script>
</head>
<body>
    <h2>Report Params</h2>
    <form:form id="reportForm" modelAttribute="attributes"
        action="searchSubmit" method="POST">
        <input type="hidden" id="status" name="status">
        Report Type : <form:select path="reportType" id="reportTypes"
            name="reportTypes" style="width:150px;" onchange="this.form.submit();">
            <form:option value="Select">--Select--</form:option>
            <form:option value="sprintPlanningReport">Sprint Planning</form:option>
            <form:option value="cycleDaysReport">Cycle Days Report</form:option>
        </form:select>
        <br />
        <br />
        <c:if test="${dropdownList.size() > 0}">
            <c:forEach items="${dropdownList}" var="dd">
                ${dd.name} :<select id="${dd.name}" name="${dd.name}" style="width:150px;">
                    <c:forEach items="${dd.options}" var="opt">
                        <option value="${opt}">${opt}</option>
                    </c:forEach>
                </select>

                <br />
                <br />
            </c:forEach>
            <br />
                <input id="btnSubmit" type="submit" name="btnSubmit" value="Submit"
                style="margin-left: 116px;" />

        </c:if>
        <c:if test="${inputList.size() > 0}">
            <c:forEach items="${inputList}" var="input">
                ${input.name} :<input type="text" id="${input.name}" name="${input.name}" style="width:150px;"/>
                <br />
                <br />
            </c:forEach>
        </c:if>
    </form:form>
</body>
</html>

当我从提到的控制器点击 /search url 时,它给出 HTTP 状态 404:请求的资源不可用。

【问题讨论】:

    标签: java xml spring-mvc


    【解决方案1】:

    我将&lt;mvc:annotation-driven/&gt;添加到我的dispatcher-servlet.xml 中并且它起作用了。

    【讨论】:

      【解决方案2】:

      建议您再次检查映射&lt;mvc:resources .../&gt; 的正确性。例如,如果您将资源放在WEB-INF 文件夹下,则应在路径中指定它,例如/WEB-INF/...

      如果没有帮助,请尝试启用默认 Servlet 处理程序,就像在 Spring MVC docs 中指定的那样。为此,请将标签 &lt;mvc:default-servlet-handler/&gt; 放入您的 dispatcher-servlet.xml。

      希望这会有所帮助。

      【讨论】:

      • 资源在 webapp 文件夹下,也尝试添加 但没用
      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多