ВЕСНА Tomcat Запрашиваемый ресурс недоступен - PullRequest
0 голосов
/ 08 июля 2019

Я запускаю Spring Mvc с простым приложением hello world, но оно не работает.

Используйте редактор Eclipse и запускайте на сервере Tomcat.

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.hello</groupId>
  <artifactId>SpringMavenHello</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMavenHello Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

    <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>3.2.4.RELEASE</version>
    </dependency>

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




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

  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>SpringMavenHello</finalName>
  </build>
</project>

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>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

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



    <listener>
    <listener-class>org.springframework.web.context.ContextLoadListener</listener-class>
    </listener>

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

    <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

dispatcher-servlet.xml

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


    <mvc:annotation-driven />
    <context:component-scan base-package="com.hello" />


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

        <property name="suffix" value=".jsp" />

    </bean>

</beans>

index.jsp

<html>
<body>
<a href="hello">click here</a>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Spring MVC</h1>
</body>
</html>

HelloController

package com.hello;

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

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public String sayHello() {
        return "welcome"; // nome pagina
    }

}

Когда я запускаюна сервере и http://localhost:8080/SpringMavenHello Я получаю эту ошибку: Запрошенный ресурс недоступен.

Помогите мне !!Пожалуйста!Спасибо!



1 Ответ

0 голосов
/ 08 июля 2019

версия пружины в pom.xml (<version>3.2.4.RELEASE</version>) и упомянутые версии схемы в диспетчере-сервлете (например: http://www.springframework.org/schema/context/spring-contex-4.0.xsd) должны быть согласованы

я удален <welcome-file-list>... из web.xml

поэтому я добавил start() -метод к HelloController

другие изменения, которые я сделал, прокомментированы в примерах исходного кода

, попробуйте следующее

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>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoadListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <!--not needed anymore -->
    <!--welcome-file-list>
        <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    </welcome-file-list-->
</web-app>

dispatcher-servlet.xml

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
    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-contex.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
    <!-- above schemaLocation for mvc edited -->
    <mvc:annotation-driven />

    <!-- .** added to also scan sub-packeges -->
    <!-- annotation-config="true" added allow annotation-based-configuration -->
    <context:component-scan
        base-package="com.hello.**"
        annotation-config="true"/>
    <bean
        id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    >
        <property
            name="prefix"
            value="/WEB-INF/jsp" />
        <property
            name="suffix"
            value=".jsp" />
    </bean>
</beans>

HelloController

package com.hello;

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

@Controller
public class HelloController {
    /**
     * the annotation {@link RequestMapping} can be shortened/replaced with
     * {@link GetMapping}
     *
     * @see {@link HelloController#start()}
     */
    // method specified
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHello() {
        return "welcome"; // nome pagina
    }

    @GetMapping("/")
    public String start() {
        return "index"; // nome pagina
    }
}

Измените

, переименовав dispatcher-servlet.xml в spring-servlet.xml, нет необходимости указывать <context-param><param-name>contextConfigLocation... в web.xml

если вы хотите сохранить «ваше» имя или следовать «вашим» соглашениям об именах, и это не работает, возможно, вы можете объявить contextConfigLocation следующим образом

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:**/dispatcher-servlet.xml
    </param-value>
</context-param>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...