【问题标题】:Unable to Setup Spring + Maven Project无法设置 Spring + Maven 项目
【发布时间】:2017-05-12 15:52:58
【问题描述】:

我是使用 Spring 的新手,因此决定学习本教程:

http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/

我的文件和文件结构与教程相匹配,index.jsp 正在工作。但是,当我单击转到 helloworld.jsp 时,我收到以下 404 错误

源服务器没有找到目标的当前表示 资源或不愿透露存在的资源。

任何人都可以建议挖掘的地方吗?不适合Tomcat 8.5的教程有什么问题吗?还是更有可能是我的设置有问题?

编辑: 我安装了以下内容:

  1. Tomcat 8.5.14
  2. 带有 Spring IDE 的 Eclipse Neon
  3. Maven 3.5.0

如果有帮助,在我尝试将它与 Web/Spring 一起使用之前,Maven 一直在工作(即 mvn install 下载正确的库)

我添加了一个image 我的文件和下面是我认为相关的文件的实际代码:

UserController.java

package com.ankurmgoyal.hellotest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {


String message = "Welcome to Spring MVC!";

    @RequestMapping("/hello")
    public ModelAndView showMessage(
            @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        System.out.println("in controller");

        ModelAndView mv = new ModelAndView("helloworld");
        mv.addObject("message", message);
        mv.addObject("name", name);
        return mv;
    }
}

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>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>

    <center>
        <h2>Hello World</h2>
        <h3>
            <a href="hello">Click Here</a>
        </h3>
    </center>
</body>
</html>

helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
    <center>
        <h2>Hello World</h2>
    </center>
</body>
</html>

调度程序-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

【问题讨论】:

  • 你能发布你的设置吗?
  • 嗨,比尔,我添加了更多信息。谢谢!

标签: spring maven jsp spring-mvc


【解决方案1】:

index.jsp 由 DefaultServlet 提供服务。调度程序的 url 模式是 /。 / 模式覆盖了 defaultServlet,所有的调用都通过这个 servlet 进行路由。将 url 模式更改为一些非空字符串并尝试一下。

请参阅此SO Question 了解更多详情。

【讨论】:

  • 非常感谢您的回复。我看起来更深一些,并尝试了您发送的链接中建议的不同 url 模式,但是在尝试这些时,我得到了同样的错误,除了现在在 index.html 上。您是否认为本质上我的问题是当我单击索引上的“单击此处”时,“/hello”没有被发送到 DispatcherServlet?
  • 你能发布你正在使用的确切代码吗?这样会更容易理解
  • 当然 - 添加了文件。再次感谢您抽出时间提供帮助。
  • 我已经尝试了确切的设置,但在 intellij 上使用了 tomcat7 maven 插件。这对我来说似乎工作得很好。如果你点击 localhost/hellotest/hello 会发生什么?您现在可以访问 helloworld.jsp 了吗?
猜你喜欢
  • 2015-02-24
  • 2021-05-28
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2013-11-06
  • 1970-01-01
相关资源
最近更新 更多