这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>someServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>`
<display-name>Archetype Created Web Application</display-name>`
</web-app>
这是我的servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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-context-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.cmpny.controller"> </context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
这是我的控制器
package com.cmpny.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/greeting")
public String sayHello(Model model){
model.addAttribute("greeting", "Hello World");
return "hello";
}
}
/WEB-INF/jsp/hello.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>Greetings</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>``
点击网址后我遇到错误
HTTP Status [404] – [Not Found]
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.0.M20
我使用的是spring-web-4.0.0.RELEASE.jar。请帮我摆脱这个错误。当我点击http://localhost:8080/SampleApplication/
时,它工作正常并显示index.jsp中的内容。但是,当我尝试http://localhost:8080/SampleApplication/greeting.html
答案 0 :(得分:-1)
以下应该工作:
web.xml中的更改:
<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
控制器的变化:
@RequestMapping(value = "/greeting.html")
public String sayHello(Model model) {
// Do something
}