0

嗨,我已经在 spring mvc 中编写了登录代码,我的代码如下:

web.xml:-

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> Spring3Demo

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/forms/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 </web-app>

Dispatcher-servlet:-

<context:component-scan base-package="net.roseindia.controllers" />

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

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages" />
</bean>
<bean id="loginForm" class="net.roseindia.forms.LoginValidator"/>
<bean id="loginController" class="net.roseindia.controllers.LoginController">
    <property name="sessionForm"><value>false</value></property>
    <property name="commandName"><value>loginForm</value></property>
    <property name="commandClass"><value>net.roseindia.forms.LoginForm</value></property>
    <property name="formView"><value>index</value></property>
    <property name="successView"><value>loginsuccess</value></property>
    <property name="urlMap">
        <map>
            <entry key="index.jsp">
                <ref bean="loginController"/>
            </entry>
        </map>
    </property>
</bean>
<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
</bean>

Index.jsp:-这是在 webinf 之外的 web 内容文件夹中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <!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=ISO-8859-1">
   <title>Spring 3, MVC Examples</title>
</head>
 <body>
    <h1>Spring 3, MVC Examples</h1>
 <ul>
   <li><a href="forms/helloworld.html">Hello World</a></li>
</ul>
<form:form action="login.html" commandName="loginForm"  >
<table>
    <tr>
    <td>User Name<FONT color="red"><form:errors path="userName" /></FONT></td>  <td><form:input path="userName" /></td>
    </tr>
    <tr>
    <td>Password<FONT color="red"><form:errors path="password" /></FONT></td><td><form:password path="password" /></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" name="submit" value="Login"/></td>
    </tr>
</table>
</form:form>
  </body>
</html>

在 src 文件夹中,我有net.roseindia.forms.LoginFormnet.roseindia.controllers.LoginController类

我收到错误 java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 在 index.jsp 的第 15 行,即在表单标签上

请建议如何配置它。

4

1 回答 1

1

将上下文参数 contextConfigLocation 添加到您的 web.xml。即使您不需要尝试添加一个空的 applicationContext 文件。

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:applicationContext*.xml</param-value> 
</context-param>
于 2013-01-24T15:45:56.640 回答