3

这些天我开始学习 Spring,我遇到了以下异常:我只是想从登录页面导航(稍后将使用 DAO 在 db 中插入操作)到某个 index.jsp 页面

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 11

8: </head>
9: <body>
10: <form:form method="post" commandName="user" action="login">
11:     <form:label path="uname" /> <form:input path="uname" />
12:     <form:label path="password" />  <form:input path="password" />
13:     <input type="submit" value="Submit" />
14: </form:form>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.login_jsp._jspx_meth_form_005flabel_005f0(login_jsp.java:157)
    org.apache.jsp.login_jsp._jspx_meth_form_005fform_005f0(login_jsp.java:111)
    org.apache.jsp.login_jsp._jspService(login_jsp.java:71)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

登录.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@ taglib
    prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTsYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<form:form method="post" commandName="user" action="login">
    <form:label path="uname" /> <form:input path="uname" />
    <form:label path="password" />  <form:input path="password" />
    <input type="submit" value="Submit" />
</form:form>
</body>
</html>

登录控制器.java

package com.bts.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/login")
@SessionAttributes("user")
public class LoginController {

    @RequestMapping(method=RequestMethod.POST)
    public String  onSubmit(){
        System.out.println("entered in submit methodd....");
        return "index";
    }
}

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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>BugTrackingSystem</display-name>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



</web-app>

User.java *强文本*

package com.bts.vo;

import java.util.ArrayList;
import java.util.List;

public class User {

    public User() {

        countries = new ArrayList<String>();

        countries.add("India");
        countries.add("US");

    }

    private String uname;
    private String password;
    private List<String> countries;

    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public List<String> getCountries() {
        return countries;
    }
    public void setCountries(List<String> countries) {
        this.countries = countries;
    }

}

应用程序上下文.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"
    xmlns:p="http://www.springframework.org/schema/p"
    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">

  <import resource="dispatcher-servlet.xml"/>
  <import resource="datasource.xml"/>
  <import resource="hibernate-beans.xml"/>


</beans>

调度程序-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"
    xmlns:p="http://www.springframework.org/schema/p"
    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">


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <context:component-scan base-package="com.bts.controller" />


</beans>

请有人可以运行我的代码并为我提供解决方案.. :)

4

1 回答 1

8

您的 JSP 中的表单标签试图绑定到一个名为user(由 表示commandName="user")的 bean,但这并未在您的控制器中作为模型属性公开。

尝试添加到您的控制器:

@ModelAttribute("user")
public User createModel() {
    return new User();
}

因为您的控制器带有@SessionAttributes("user")模型注释,所以它会在第一次创建时存储在会话中。后续请求将从会话中提取模型。

于 2013-05-28T10:00:21.490 回答