SpringMVC入门案例出现网页404错误的问题 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。

SpringMVC入门案例出现网页404错误的问题

代码部署

目的:Tomcat服务器实现通过控制类(HelloController)来进行一个页面(index.jsp)向另一个页面(success.jsp)跳转
项目的目录:SpringMVC入门案例出现网页404错误的问题 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。_第1张图片

代码

Hellocontroller

package cn.itcast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 控制器类
 */
@Controller
@RequestMapping(path = "/T")
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello()
    {
        System.out.println("Hello SpringMVC");
        return "success";
    }

    @RequestMapping(path = "/RM")
    public String textRequestMapping()
    {
        System.out.println("textRequestMapping()方法执行中......");
        return "RMtest";
    }
}

springmvc.xml




    
    

    
    
        
        
    
    
    


success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


入门成功

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--加载配置文件下的配置-->
    <load-on-startup>1</load-on-startup>
    <!--启动服务器九开始创建DispatcherServlet-->
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

index.xml

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<a href="T/hello">入门程序</a>
<br/>
<a href="T/RM">textRequestMapping() Test</a>
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.itcast</groupId>
  <artifactId>springmvc_day01_01_start</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc_day01_01_start Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>



  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>

  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>


    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>springmvc_day01_01_start</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>




</project>

以上完整代码成功部署在Tomcat,运行后出现以下问题在这里插入图片描述
要注意的是index.jsp页面不能写在WEB-INF目录下,否则无法加载出来会找不到资源SpringMVC入门案例出现网页404错误的问题 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。_第2张图片
接着出现了以上问题,这里要注意的是你的Hellocontroller类返回字符串会通过视图解析器拼接成“返回字符串”+“.jsp”页面,返回字符串与页面名一致
SpringMVC入门案例出现网页404错误的问题 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。_第3张图片
此处href不能写成 “/T/hello” 否者也会报错,而应该把T前面的“/”给去掉:SpringMVC入门案例出现网页404错误的问题 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。_第4张图片

你可能感兴趣的:(Java)