web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
        <taglib-uri>http://www.springframework.org/tags/form</taglib-uri>
        <taglib-location>/WEB-INF/spring-form.tld</taglib-location>
    </taglib>
  <taglib>
        <taglib-uri>http://www.springframework.org/tags</taglib-uri>
        <taglib-location>/WEB-INF/spring.tld</taglib-location>
    </taglib>

</jsp-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
dispatcher.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="no" default-lazy-init="false"
default-dependency-check="none">
<bean name="/test.html" class="com.test.TestController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean name="/login.do" class="com.test.LoginController">
<property name="userService">
<ref bean="userService"/>
</property>
<property name="commandClass" value="com.test.bo.User"></property>
<property name="formView" value="login"/>
<property name="successView" value="loginSuccess"/>
</bean>
<bean id="userService" class="com.test.bo.UserServiceImp"></bean>
</beans>
loin.jsp的配置
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'login.jsp' starting page</title>
      </head>
    <body>
   login <br>
 <form:form method="post" action="/login.do">
 
  name<form:input path="username"/>
  <br><form:errors path="username" />
  pass<form:input path="password"/>
  <br><form:errors path="password" />
  <input type="submit" name="submit" value="登录">
 </form:form>
</body>
</html>
LoginController的配置
package com.test;
import java.util.HashMap;
import java.util.Map;
import com.test.bo.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;public class LoginController extends SimpleFormController
{
private UserService userService;

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
{
// TODO 自动生成方法存根
User user=(User)command;
try
{
userService.login(user.getUsername(), user.getPassword());
request.getSession().setAttribute("USERNAME", user.getUsername());
System.out.println("xxxxxx");
Map model=new HashMap();
model.put("username",user.getUsername());
return new ModelAndView(getSuccessView(),model);

}
catch(RuntimeException e)
{
Map model=new HashMap();
model.put("command", command);
model.put("error", e.getMessage());
return new ModelAndView(getFormView(),model);
}

//return super.onSubmit(request, response, command, errors);
} public void setUserService(UserService userService)
{
this.userService = userService;
}
运行IE输录http://localhost:8080/testSpringMvc/login.jsp
显示错误严重: No WebApplicationContext found: no ContextLoaderListener registered?
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

2008-10-20 9:36:07 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
请各位高手帮忙解决!!谢谢

解决方案 »

  1.   

    <servlet-mapping>
    <servlet-name>dispatcher </servlet-name>
    <url-pattern>*.html </url-pattern>
    </servlet-mapping> 
    沒有JSP加
    <servlet-mapping>
    <servlet-name>dispatcher </servlet-name>
    <url-pattern>*.jsp </url-pattern>
    </servlet-mapping> 
      

  2.   

    你应该是在整合struts和spring吧
    要在web.xml里加上加载上下文的配置- <context-param>
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath*:applicationContext*.xml</param-value> 
      </context-param>
    - <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
      </listener>