DispatcherServlet没有被调用?

时间:2016-06-16 19:22:26

标签: java spring model-view-controller

<?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/j2ee"     
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID"  
version="2.4">
 <servlet>
   <servlet-name>welcome</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>

<servlet-mapping>
 <servlet-name>welcome</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 </web-app>

的index.jsp

<html>
<body>
<form action="add"> 
    <input type="text" name="one"/><br>
    <input type="text" name="two"/><br>
    <input type="submit"/>
</form>
</body>
</html>

欢迎-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.ashish.springMVC" />

<bean   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean> 
</beans>

控制器

package com.ashish.springMVC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Java4sController {

@RequestMapping("/*")
public ModelAndView helloWorld() {
 System.out.println("hello....");
 String message =  "Welcome to Java4s.com Spring MVC 3.2.x Sessions";
 message += "<br>You Did it....!";

 return new ModelAndView("welcomePage.jsp", "welcomeMessage", message);
 }//ModelAndView closed

 }

welcomePage

<html>
<body>
<font face="verdana" size="2">
   ${welcomeMessage}
</font>
</body>
</html>

我想运行它,但代码访问DispatcherServlet,表现得像JSP 我尝试执行此操作,但它给出404-没有资源/添加发现错误。请建议一些编辑以使其正确运行。

1 个答案:

答案 0 :(得分:0)

您的方法是分析问题场景并尝试最小化问题区域

  • 首先检查请求是否到达控制器。
  • 如果请求没有到达控制器,那么你需要弄清楚欢迎-servlet.xml放在正确位置的原因是什么
  • 如果请求到达控制器,则检查helloWorld()内是否存在问题,例如您返回welcomePage.jsp new ModelAndView("welcomePage.jsp", "welcomeMessage", message);应为new ModelAndView("welcomePage", "welcomeMessage", message);,因为internalResourceViewResolver只需要名称。