0

I am using spring MVC3.0 . Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>Spring MVC Form Handling</display-name>

  <servlet>
    <servlet-name>spring</servlet-name>
       <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
      </servlet-mapping>

<servlet-mapping>
    <servlet-name>spring1</servlet-name>
    <url-pattern>/</url-pattern>
      </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>

Note:I have two url patterns thats why the app designing is breaking and I am getting the 404 Error for css and images etc...

spring-servlet.xml:

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </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="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
            </bean> 

so I wants have the resourse handler bean for the problem how to do that.

4
  • 1
    Why do you need two Spring servlets? Is there some compelling reason? Commented Apr 9, 2014 at 14:17
  • Please show the definition of spring1 servlet. Commented Apr 9, 2014 at 14:48
  • 1
    I would suggest reading the chapter of the Spring Docs on static resource handling: docs.spring.io/spring/docs/4.0.3.RELEASE/… Commented Apr 9, 2014 at 16:31
  • And upgrade to Spring 4. 3.0 is ancient. Commented Apr 9, 2014 at 21:33

2 Answers 2

2

Make use of Spring feature that has efficient way of serving static resorces. For that purpose you should put the below entry spring-context.

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

you must place your css,js and images files in webappRoot/resources folder. Also remove the servlet mapping for serving static resources. In your case keep the below servlet mapping and remove the spring1 servlet from web.xml because it is not mapped with any target servlet

<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

In JSP, refer static resources by
<link rel="stylesheet" type="text/css" media="screen" href="<s:url value='/resources/css/styles.css'/>" /> <script src="<s:url value='/resources/js/jquery-1.8.3.js'/>" type="text/javascript"> </script>

Sign up to request clarification or add additional context in comments.

2 Comments

,I am not able to add the <mvc:resources/> tag in spring-servlet.xml file.
0

Check your logs, you should have got an exception on the lines:

    java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name spring1...

Unless you missed out another servlet with servlet-name spring1, remove the below lines from web.xml and check:

    <servlet-mapping>
        <servlet-name>spring1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.