Adrian Daniel Culea Adrian Daniel Culea - 1 month ago 9
Java Question

Spring - The requested resource is not available

I am trying to build a basic Java app with Spring MVC, however, I am having troubles with links within the app.

Here is my

web.xml


<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
</servlet-mapping>




My
test-servlet.xml


<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.springtest.controller"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">

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

</bean>




The first page that gets displayed when running the app is the following:

<html>
<head>
<title>Spring MVC Tutorial</title>
</head>
<body>
<br>
<div style="text-align:center">
<h3>
<a href="welcome">Click to say hello </a>
</h3>
</div>
</body>
</html>


And welcome.jsp is this:

<html>
<head>
<title>Hello World MVC</title>

</head>
<body>${message}

</body>
</html>


Since this is my first time using Spring (and I know there are a lot of questions here asking/answering this - thus I haven't been able to find an answer that helps me), I followed an online tutorial.

My folder structer is similar to what they have in the tutorial, however, every time I click the link to go to the second page, I get a 404 Error saying that:

/SpringTutorial-1.0-SNAPSHOT/WEB-INF/jsp/welcome.jsp - is not available

Sam Sam
Answer
  1. Basically / in the <url-pattern> tag is enough.It will take of /welcome.jsp or /welcome.html.

    <servlet-mapping>
       <servlet-name>test</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  2. No need to give relative path in value property of prefix.

    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    

And one more thing is you can also try with InternalResourceViewResolver in place of UrlBasedViewResolver.

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