【问题标题】:What is the problem in sample spring mvc application示例 spring mvc 应用程序中的问题是什么
【发布时间】:2020-01-27 02:20:41
【问题描述】:

我在 spring mvc 中比较新,我开发了一个示例 helloworld 应用程序,但是在将一个 jsp 页面导航到另一个 jsp 页面时出错。

**maven项目创建时提供的信息

**项目文件夹结构

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <display-name>Archetype Created Web Application</display-name>
 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
    <servlet-name>practice1</servlet-name>
     <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
     <load-on-startup>1</load-on-startup>
 </servlet>

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

practice1-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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package="com.practice1.controller" />

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

HelloWorldController.java

package com.practice1.controller;

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

@Controller
public class HelloWorldController {
    @RequestMapping("/helloworld")
    public ModelAndView hello() {
        String helloWorldMessage = "Hello world from practice1!";
        return new ModelAndView("hello", "message", helloWorldMessage); // 1st is view name 2nd is model name, 3rd is model Object
   }
}

pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.practice1</groupId>
<artifactId>parctice1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>practice1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${javax.version}</version>
    </dependency>

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

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

</dependencies>

<build>
    <finalName>practice1</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<properties>
    <spring.version>5.2.3.RELEASE</spring.version>
    <mysql.version>5.1.36</mysql.version>
    <javax.version>3.1.0</javax.version>
    <junit.version>3.8.1</junit.version>
    <jdk.version>1.8</jdk.version>
</properties>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>HelloWorld</title>
  </head>
 <body>
   <a href="helloworld">Click here to read hello message </a>
 </body>

你好.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>Hello</title>
</head>
<body>${message}
</body>

每当单击索引页面的超链接导航到 hello 页面时都会出现以下错误。

还有如下所示的 Eclipse 控制台日志;

信息:服务器在 [10,576] 毫秒内启动 2020 年 1 月 26 日晚上 11:12:02 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:GET /practice1/helloworld 没有映射

【问题讨论】:

  • 更新项目文件夹结构和类路径后,它的工作查找。

标签: java spring


【解决方案1】:

您需要在链接前添加上下文路径。

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

...
<a href="${contextPath}/helloworld">Click here to read hello message</a>

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2011-07-05
    • 2014-01-27
    相关资源
    最近更新 更多