2

spring mvc + maven + jpa hibernate を使用して簡単なプロジェクトを作成しようとしています。

私のjspページは次のとおりです。

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div>
<form:form action="contact.html" method="post" commandName="contact" >

<div>
<label for="firstname">First Name</label>
<form:input path="firstname"/>
<form:errors path="firstname" cssClass="error"/>
</div>

<div>
<label for="lastname">Last Name</label>
<form:input path="lastname"/>
<form:errors path="lastname" cssClass="error"/>
</div>

<div>
<label for="telephone">Telephone</label>
<form:input path="telephone"/>
<form:errors path="telephone" cssClass="error"/>
</div>

<div>
<label for="email">Email</label>
<form:input path="email"/>
<form:errors path="email" cssClass="error"/>
</div>

<div>
<form:button name="submit" value="submit">Add Contact</form:button>
</div>

私の contact.html コントローラー:

package com.corasent.contacts.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.corasent.contacts.form.Contact;
import com.corasent.contacts.service.ContactService;

@Controller
@RequestMapping("contact.html")
public class ContactManagerController {


@Autowired
ContactService cs;

@RequestMapping(method = RequestMethod.GET)
public String contactController()
{
    System.out.println("In controller");
    ModelMap map=new ModelMap();
    Contact contact=new Contact();
    map.addAttribute("contact", contact);
    return "contacts";
}

@RequestMapping(method=RequestMethod.POST)
public ModelAndView getInfo(@ModelAttribute Contact contact, BindingResult result, HttpServletRequest req)
{
    ModelAndView mv=new ModelAndView("contacts");
    String name=contact.getFirstname();
    System.out.println("name is="+name);
    cs.validate(contact, result);
    if(result.hasErrors())
    {

        return mv ;
    }

    System.out.println("in post method");
    return mv;


}

public ContactService getCs() {
    return cs;
}

public void setCs(ContactService cs) {
    this.cs = cs;
}

}

jsp の commandName は、コントローラーに渡す必要がある Bean であることを知っています。同じ Bean がコントローラーで開始されますが、機能していないようです。上記のエラーが表示されます。私もstackTraceを貼り付けています。

ERROR: org.springframework.web.servlet.tags.form.InputTag - Neither BindingResult nor     plain target object for bean name 'contact' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' 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:179)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
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:103)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspx_meth_form_005finput_005f0(contacts_jsp.java:272)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspService(contacts_jsp.java:100)
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.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
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:99)
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:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
12 Apr, 2013 12:36:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/contacts] threw     exception [An exception occurred processing JSP page /WEB-INF/views/jsp/contacts.jsp at line    10

7: 
8: <div>
9: <label for="firstname">First Name</label>
10: <form:input path="firstname"/>
11: <form:errors path="firstname" cssClass="error"/>
12: </div>

前もって感謝します

4

2 に答える 2

2

試してみてください:

@RequestMapping(method = RequestMethod.GET)
public String contactController(Model model)
{
    System.out.println("In controller");
    // ModelMap map=new ModelMap();
    Contact contact=new Contact();
    model.addAttribute("contact", contact);
    return "contacts";
}

コードでは、ModelMap を作成していますが、メソッドの最後で破棄されているため役に立ちません。モデルをパラメーターとして渡すと、Spring は後でそれを使用して JSP を埋めます。

ここに例があります。

于 2013-04-12T08:12:16.230 に答える