我尝试在Spring MVC上开始开发,只是一个小项目,但得到以下异常:
找不到ServletContext资源[/WEB-INF/helloWorld-servlet.xml]中定义名称为'/hello.ds'的bean的类[controller.HelloController];嵌套异常是java.lang.ClassNotFoundException:controller.HelloController
(我的HelloController.java类在src->控制器包下)
这是我的 index.jsp
<h1>HelloWorld</h1>
<form action="./hello.ds">
Name:<input type="text" name="name">
<input type="submit" value="sayHello">
</form>
这是我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>MVC First Project</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
这是我的 helloWorld-servlet.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="/hello.ds" class="controller.HelloController"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这是我的控制器: Hello Controller.java
package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
// TODO Auto-generated method stub
String name=req.getParameter("name");
Map m=new HashMap();
m.put("msg","hello.." +name);
ModelAndView mav=new ModelAndView("success",m);
return mav;
}
}
这是我的 success.jsp
${msg}
罐子补充道:
com.springsource.org.apache.commons.logging-1.1.1
com.springsource.org.apache.log4j-1.2.15
jmxtools-1.2.1
org.springframework.asm-3.0.1.RELEASE-A
org.springframework.beans-3.0.1.RELEASE-A
org.springframework.context-3.0.1.RELEASE-A
org.springframework.core-3.0.1.RELEASE-A
org.springframework.expression-3.0.1.RELEASE-A
org.springframework.web-3.0.1.RELEASE-A
org.springframework.web.servlet-3.0.1.RELEASE-A