![](/img/trans.png)
[英]org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /HelloWorld/helloweb
[英]org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /springMVCDemo/createAccount.html
这是我的 package:
package com.demo.controllers;
import java.util.Random;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MyDemoController {
private String[] quotes = {"To be or not to be -Shakespeare", "Spring is the nature's way of
saying 'let's party'-from someone","The time is always right to do what is right"};
@RequestMapping(value="/getQuote")
public String getRandomQuote(Model model) {
int rand = new Random().nextInt(quotes.length); String randomQuote =quotes[rand];
model.addAttribute("randomQuote", randomQuote);
return "quote"; }
@RequestMapping(value="/createAccount")
public String createAccount() {
return "createAccount";
}
}
这是我的 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_3_0.xsd"
version="3.0">
<display-name>My Demo App</display-name>
<servlet>
<servlet-name>MyDemoApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyDemoApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
******servlet.config*****
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.demo.controllers"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property> </bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <bean name="/getQuote.html" class="com.demo.controllers.MyDemoController" /> <bean name="/createAccount.html" class="com.demo.controllers.MyDemoController" />
</beans>
我的报价。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>My Random Quote page</title>
</head>
<body>
<h1>The Quote is:</h1>
<p>${randomQuote}</p>
</body>
</html>
我的 createAccount.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>Create an Account</title>
</head>
<body>
<h1>Enter Account Details</h1>
<h2>Enter your details</h2>
<form>
<table>
<tr><td>First Name:<input type="text" name= "firstname" /></td></tr>
<tr><td>Last Name:<input type="text" name= "lastname" /></td></tr>
<tr><td>Address:<input type="text" name= "address" /></td></tr>
<tr><td>Email:<input type="text" name= "email" /></td></tr>
<tr><td><input type="submit" value= "Create " /></td></tr>
</table>
</form>
</body>
</html>
但我没有得到 localhost:8080/springMVCDemo/createAccount.html 的页面
任何线索都会有所帮助
来自Spring 框架参考:
对于没有
@RequestMapping
方法声明的 @RequestMapping,Allow
header 设置为GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS
。 Controller 方法应始终声明支持的 HTTP 方法(例如,通过使用 HTTP 方法特定变体:@GetMapping
、@PostMapping
等)。
因此,您应该明确提及处理程序允许的方法以实现预期行为。 所以,改为:
@RequestMapping(value = "/getQuote", method = RequestMethod.GET)
@RequestMapping(value = "/createAccount", method = RequestMethod.GET)
错误背后的原因是 Tomcat,我们需要构建并重新发布才能正确加载所有页面
尝试清理 Tomcat 工作目录并重新运行,它对我有用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.