tanaji mane tanaji mane - 4 months ago 24
Java Question

Added "mvc:resources" tag to dispatcher-servlet.xml to add js and css, it is giving HTTP Status 404


I am developing a spring-MVC application.It was working fine but as I added "mvc:resources" tag to dispatcher-servlet.xml to add js and css it is giving HTTP Status 404. My dispatcher-servlet.xml is as below:


<?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>



And my web.xml is:


<?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>



The controller code is:


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 is:


<%@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>



When I am hitting /search url from mentioned controller, it is giving HTTP Status 404:The requested resource is not available.

Answer

I added <mvc:annotation-driven/>to my dispatcher-servlet.xml and it worked.

Comments