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

时间:2018-04-06 13:39:10

标签: spring model-view-controller config

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

message /hello.do

说明请求的资源不可用。

Apache Tomcat / 7.0.85

那么网址是:http://localhost:8080/hello.do 我必须输入完整的路线: http://localhost:8080/SpringMVC/hello.do让它发挥作用。 我认为我的web.xml和SpringMVC-servlet.xml配置中必定存在一些错误。我在下面发布了我的所有代码并欢迎任何建议。

的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>

1 个答案:

答案 0 :(得分:0)

您可以更改<form action>,如下所示。这是一种更好的方法。

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