0

我的主页有两个超链接

<body>
<pre>

    <font face="Times New Roman" size="20"><a href="logincontrol.htm">Login</a></font>

    <font face="Times New Roman" size="20"><a href="registercontrol.htm">Register</a></font>

</pre>

处理这个页面的控制器是......

package core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MainController extends MultiActionController {

    public ModelAndView logincontrol(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("Add method called");
        return new ModelAndView("LoginPage");
    }

    public ModelAndView registercontrol(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("Register method called");
        return new ModelAndView("RegisterPage");
    }
}

然后当我点击主页上的超链接登录时,它会抛出异常

我的 LoginPage.jsp 文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<body>

<f:form method="post" commandName="POJO">
    <pre>
        User Name : <f:input path="username" />
        Password : <f:input path="password" />
    </pre>
</f:form>

<%-- <div>The request scope is: ${requestScope}</div>
<div>The command is: ${requestScope.POJO}</div> --%>
</body>
</html>

我的控制器代码

package core;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginController extends SimpleFormController{


    public LoginController() {
//      // TODO Auto-generated constructor stub
        setCommandClass(LoginPojo.class);
        setCommandName("POJO");
    }

@Override
protected ModelAndView onSubmit(Object command) throws Exception {

    System.out.println("Inside mainController "+command.toString());    
    return new ModelAndView("LoginPage");

}   

}

我有一个名为 LoginPojo 的 Pojo 类

package core;

public class LoginPojo {

    String username;
    String password;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

在移动到 LoginPage.jsp 时,我收到 BindingResult 错误。帮助....

错误:

Jan 27, 2013 12:02:00 AM org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag
SEVERE: Neither BindingResult nor plain target object for bean name 'POJO' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'POJO' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    at org.apache.jsp.view.LoginPage_jsp._jspx_meth_f_005finput_005f0(LoginPage_jsp.java:157)
    at org.apache.jsp.view.LoginPage_jsp._jspx_meth_f_005fform_005f0(LoginPage_jsp.java:118)
    at org.apache.jsp.view.LoginPage_jsp._jspService(LoginPage_jsp.java:79)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
4

1 回答 1

2

LoginPojo使用 attributeName 向模型添加一个实例POJO。这将允许 Spring 标记建立数据绑定。即使您显示一个空白表单来插入记录,模型仍然需要包含一个可用于数据绑定的对象。

public class MainController extends MultiActionController {
    public ModelAndView logincontrol(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("Add method called");
        return new ModelAndView("LoginPage", "POJO", new LoginPojo());
    }
    //...
}
于 2013-01-26T18:19:56.497 回答