【问题标题】:Spring MVC - 404 - The origin server did not find a current representationSpring MVC - 404 - 源服务器未找到当前表示
【发布时间】:2019-10-26 19:05:05
【问题描述】:

我正在尝试部署 Spring MVC Web 应用程序。 我想将我的 index.jsp 重定向到另一个 .jsp,但出现此错误。

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.

我的项目结构:

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

当我点击测试时,我在 sts 日志中收到此错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:GET /example/hello 没有映射

【问题讨论】:

    标签: spring spring-mvc tomcat9


    【解决方案1】:

    您应该使用“/”来映射您的控制器。

    你可以试试这个:

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

    而且链接最好使用相对路径:

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

    更多关于pageContext see this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2021-10-02
      • 2021-10-20
      • 2019-09-29
      相关资源
      最近更新 更多