我的 Spring MVC 3.0 配置有什么问题?

what is wrong with my Spring MVC 3.0 configuration?

我是 Java EE Spring MVC 编码领域的新手。当我配置我的第一个 Spring MVC 3.0 站点时,我遇到了一个奇怪的问题,我必须手动键入名为 url 的 MVC 路由才能使其工作。 在我的示例中,完整的 url 路由是: http://localhost:8080/SpringMVC/hello.jsp 我想向控制器发送一个词并将其显示在视图上。 但是当我点击 return 时,错误页面显示: HTTP 状态 404 - /hello.do 输入状态报告

留言/hello.do

说明请求的资源不可用。

Apache Tomcat/7.0.85

所以 url 路线是:http://localhost:8080/hello.do 我必须输入完整的路线: http://localhost:8080/SpringMVC/hello.do 让它工作。 我认为我的 web.xml 和 SpringMVC-servlet.xml 配置中一定有一些错误。我 post 下面是我的所有代码,欢迎提出任何建议。

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>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

SpringMVC-servlet.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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="     
           http://www.springframework.org/schema/beans     
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
   
     <!-- 配置上传文件的参数 -->
     <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="maxUploadSize" value="209715200" />     
        <property name="defaultEncoding" value="UTF-8" />  
        <property name="resolveLazily" value="true" />  
     </bean> 
     <!-- 配置Controller -->
     <bean name="/hello.do" class="com.yyy.controller.HelloController"></bean>
     <!-- 配置视图解析器 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/"></property>
          <property name="suffix" value=".jsp"></property>
     </bean>
        
</beans>

HelloController.java:

package com.yyy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController{
   
  
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
     HttpServletResponse response) throws Exception {
    String hello = request.getParameter("hello");
    System.out.println("------:" + hello);
    ModelAndView mav =  new ModelAndView("index");
    mav.addObject("helloworld", "hello    "+hello);
    return mav;
   }
  
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    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=utf-8">
<title>Insert title here</title>
</head>
<body>
 <form action="hello.do" method="post">
   hello:<input type="text" name="hello"/>
   <input type="submit" value="submit"> 
 </form>
</body>
</html>

和 index.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    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=utf-8">
<title>index.jsp</title>
</head>
<body>
<h1>${helloworld} </h1>
</body>
</html>

您可以像下面这样更改 <form action>。这是一个更好的方法。

<form action="${pageContext.request.contextPath}/hello.do">