1

Im trying to deploy a Spring MVC web app. I want to redirect my index.jsp to another .jsp but i get this error.

HTTP Status 404 – Not Found

--------------------------------------------------------------------------------

Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

My project Struture:

  • src/main/java
    • com.example.beans
    • com.example.controller
    • com.example.dao
  • src/main/webapp/
    • WEB-INF
    • /jsp
      • test.jsp
    • spring-servlet.xml
    • web.xml
    • index.jsp

spring-servlet.xml

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

    <!-- Provide support for component scanning -->
    <context:component-scan
        base-package="com.example />

    <!--Provide support for conversion, formatting and validation -->
    <mvc:annotation-driven />

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

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

    <bean id="ds"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/dbexample></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <bean id="dao" class="com.example.dao.ObjectDao">
        <property name="template" ref="jt"></property>
    </bean>

</beans>

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/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Example</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>/</url-pattern>
    </servlet-mapping>

</web-app>

Controller

@Controller
public class MainController {

    @RequestMapping("hello")
    public String redirect() {
        return "viewpage";
    }

    @RequestMapping("/")
    public String display() {
        return "index";
    }

    @RequestMapping("test")
    public String test() {
        return "test";
    }

index.jsp

<html>
<body>
<h2>Hello World!</h2>


<a href="test">View test</a>  
<a href="hello">Click here...</a>  

</body>
</html>

When i clik on test I get this error in the sts log:

org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /example/hello

Alberto
  • 35
  • 5

1 Answers1

0

You should use '/' for mapping for your controllers.

You could try this:

@RequestMapping("/example/hello")
    public String redirect() {
        return "viewpage";
    }

And it's better to use relative path for links:

<a href="${pageContext.request.contextPath}/test">View test</a> 
<a href="${pageContext.request.contextPath}/hello">Click here...</a> 

More about pageContext see this

Anthony
  • 571
  • 3
  • 11