0

这是我的控制器

@Controller
public class MyController {

    @Autowired
private LoginService  loginService;

@RequestMapping("/login.do")
public String login() {     
    return "login";
}
@RequestMapping("/validate.do")
public String validate(HttpServletRequest request,ModelMap model) {
    String name=request.getParameter("uname1");
    String pwd=request.getParameter("pwd1");
    boolean b=loginService.validate(name,pwd);
    if (b){
        model.addAttribute("name", name);
        return "welcome";
    }
    else
        return "login";
}
@RequestMapping("/register.do")
public String register() {
    return "register";
}

@RequestMapping("/registration.do")
public String registration(HttpServletRequest request, @ModelAttribute("modelObject") Login entity, BindingResult result) {
    String uname=entity.getUserName();
    String pwd=entity.getPassword();
    String sex=entity.getSex();
    String dob=entity.getDob();
    String nation=entity.getNation();
    Login log=new Login();
    log.setUserName(uname);
    log.setPassword(pwd);
    log.setSex(sex);
    log.setDob(dob);
    log.setNation(nation);  
    loginService.register(log);
    return "login";
}

}

这是jsp页面

<form:form commandName="modelObject" action="registration.do" method="post" >
    <table>
        <tr>
        <td><form:label path="userName">User Name</form:label></td>
        <td><form:input path="userName"></form:input></td>
        </tr>
        <tr>
        <td><form:label path="password">Password</form:label></td>
        <td><form:input path="password"></form:input></td>
        </tr>
        <tr>
        <td><form:label path="sex">Sex</form:label></td>
        <td><form:radiobutton path="sex" value="M" label="M"></form:radiobutton>
        <form:radiobutton path="sex" value="F" label="F"></form:radiobutton></td>
        </tr>
        <tr>
        <td><form:label path="dob">Date of Birth</form:label></td>
        <td><form:input path="dob"></form:input></td>
        </tr>       
        <tr>
        <td><form:label path="nation">Nationality</form:label></td>
        <td><form:select path="nation">
        <form:option value="0" label="Please Select"></form:option>
        <form:options items="${countryList}" itemValue="id" itemLabel="name" />
        </form:select></td>
        </tr>
        <tr>
        <td align="right"><input type="submit" value="Submit Form"></input></td>
        <td align="center"><input type="reset" value="Clear Form"></input></td>
        </tr>
    </table>    
</form:form> 

与他们一起,我收到以下错误

Feb 11, 2013 5:08:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mode] in context with path [/SpringHb] threw exception [An exception occurred processing JSP page /WEB-INF/jsp/register.jsp at line 16

13: <form:form commandName="modelObject" action="registration.do" method="post" >
14:     <table>
15:         <tr>
16:         <td><form:label path="userName">User Name</form:label></td>
17:         <td><form:input path="userName"></form:input></td>
18:         </tr>
19:         <tr>


Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'modelObject' 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:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
    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.WEB_002dINF.jsp.register_jsp._jspx_meth_form_005flabel_005f0(register_jsp.java:282)
    at org.apache.jsp.WEB_002dINF.jsp.register_jsp._jspService(register_jsp.java:114)
    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:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    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:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    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:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
    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)

我无法理解问题所在

上面使用 spring 表单标签编写的代码显示了上述错误,但是如果我使用 html 编写相同的代码,它工作正常 下面是用 jsp 编写的 html 代码

<c:url value="/registration.do" var="reg" />
<form:form action="${reg}" >
User Name <input type="text" name="userName"/><br>
Password<input type="password" name="password"/><br>
Sex<input type="radio" name="sex" value="M"/>M<input type="radio" name="sex" value="F"/>F<br>
DOB<input type="text" name="dob"/><br>
Nationality<select name="nation">
  <option value="India">India</option>
  <option value="USA">USA</option>
</select><br>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form:form>    

<c:url value="/login.do" var="log" />
<form:form action="${log}" >
<input type="submit" name="Go Back" value="Go Back" />
</form:form>

请帮忙....

4

1 回答 1

0

您需要在控制器方法中绑定空Login对象,从该方法返回指定的 jsp 页面 Somehting 像这样。

model.addAttribute("modelObject", yourEmptyModelObject);

而且只有那些您希望用户输入的字段才需要另一件事路径。因此无需为<form:label>其他不可渲染字段指定路径。

于 2013-02-11T12:30:40.670 回答