[英]SPRING Tomcat The requested resource is not available
我使用一个简单的hello world应用程序启动Spring Mvc,但无法正常工作。
使用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上运行时,我收到此错误消息:请求的资源不可用。
帮我!! 请! 谢谢!
pom.xml
( <version>3.2.4.RELEASE</version>
)中的spring-version和dispatcher-servlet中引用的scheme-version(例如: http://www.springframework.org/schema/context/spring-contex-4.0.xsd
: http://www.springframework.org/schema/context/spring-contex-4.0.xsd
)应该保持一致
我从web.xml
删除了<welcome-file-list>...
因此我将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
,无需在web.xml
声明<context-param><param-name>contextConfigLocation...
如果您要保留“您的”名称或遵循“您的”命名约定,但它不起作用,则可能是这样声明contextConfigLocation
的方法
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:**/dispatcher-servlet.xml
</param-value>
</context-param>
另一个编辑
你能解决你的问题吗?
有些步骤对我来说是例行的,我没有想到
要使用Maven和Spring启动“基本”动态Web项目,我在Eclipse中执行以下操作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.