运行以下代码时遇到一些问题:
Web.xml中
<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>To do List</display-name>
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
<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/todo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
-------------- todoservlet.xml -------------
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.in28minutes" />
<mvc:annotation-driven />
<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>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
</beans>
-------------- login.jsp -------------
<html>
<head>
<title>Login Page</title>
</head>
<body>
<p><font color="red">${errorMessage}</font></p>
<form action="login.do" method="POST">
Name : <input name="name" type="text" />
Password : <input name="password" type="password" />
<input type="submit" />
</form>
</body>
</html>
-------------- Controller -------------
package com.in28minutes.login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("name")
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginPage() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String handleUserLogin(ModelMap model, @RequestParam String name,
@RequestParam String password) {
if (!loginService.validateUser(name, password)) {
model.put("errorMessage", "Invalid Credentials");
return "login";
}
model.put("name", name);
return "welcome";
}
}
--------------收到错误信息-------------
URL: http://localhost:8080/in28Minutes27-springmvc/
------------browser error --------------
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.39
----------------- log on console ---------------------
TRACE 2016-12-20 12:14:32,152 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Bound request context to thread: org.apache.catalina.connector.RequestFacade@df024db
DEBUG 2016-12-20 12:14:32,153 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/in28Minutes27-springmvc/]
TRACE 2016-12-20 12:14:32,155 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@1a6fbdb3] in DispatcherServlet with name 'dispatcher'
DEBUG 2016-12-20 12:14:32,156 [http-nio-8080-exec-2] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /login.do
DEBUG 2016-12-20 12:14:32,156 [http-nio-8080-exec-2] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/login.do]
TRACE 2016-12-20 12:14:32,157 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@24b1f9e9] in DispatcherServlet with name 'dispatcher'
TRACE 2016-12-20 12:14:32,158 [http-nio-8080-exec-2] org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping - No handler mapping found for [/login.do]
TRACE 2016-12-20 12:14:32,158 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1f358bf7] in DispatcherServlet with name 'dispatcher'
TRACE 2016-12-20 12:14:32,159 [http-nio-8080-exec-2] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - No handler mapping found for [/login.do]
WARN 2016-12-20 12:14:32,159 [http-nio-8080-exec-2] org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/in28Minutes27-springmvc/] in DispatcherServlet with name 'dispatcher'
TRACE 2016-12-20 12:14:32,159 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@df024db
DEBUG 2016-12-20 12:14:32,159 [http-nio-8080-exec-2] org.springframework.web.servlet.DispatcherServlet - Successfully completed request
TRACE 2016-12-20 12:14:32,159 [http-nio-8080-exec-2] org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/in28Minutes27-springmvc/]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[23ms]; status=[OK]
我无法解决这个问题;任何帮助表示赞赏。
答案 0 :(得分:0)
web.xml文件中的问题。你不能使用.do扩展名作为欢迎文件,它应该是有效的扩展名文件,即.html,.jsp等
替换web.xml文件中的以下代码块
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
希望它对你有用。
更多详情:
当您没有任何处理程序方法来处理该路径时。加一个。或者在部署描述符中添加<welcome-file>
,即。 web.xml。
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
假设index.jsp位于WAR的根,即。在WEB-INF之外。