org.apache.jasper.JasperException:/WEB-INF/views/home.jsp(line:[25],column:[1])无法找到属性的setter方法:[commandName]

时间:2017-10-28 11:13:40

标签: spring jsp spring-mvc

我创建了一个弹簧形式,并希望从中提交并显示另一个jsp页面。当我运行这个项目时,我得到以下异常。任何人都可以帮助我理解为什么我会收到这个错误。

org.apache.jasper.JasperException: /WEB-INF/views/home.jsp (line: [25], column: [1]) Unable to find setter method for attribute: [commandName]
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:292)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:115)
    org.apache.jasper.compiler.Generator$GenerateVisitor.evaluateAttribute(Generator.java:2998)
    org.apache.jasper.compiler.Generator$GenerateVisitor.generateSetters(Generator.java:3218)
    org.apache.jasper.compiler.Generator$GenerateVisitor.generateCustomStart(Generator.java:2404)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1894)
    org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1544)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2389)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2441)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2447)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:470)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2389)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3657)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:256)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:384)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:345)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:312)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1325)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1069)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

JSP文件:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
    <title>Home</title>
    <style>
.error {
    color: #ff0000;
}

.errorblock {
    color: #000;
    background-color: #ffEEEE;
    border: 3px solid #ff0000;
    padding: 8px;
    margin: 16px;
}
</style>
</head>
<body>
<h2>Spring's form textbox example</h2>

    <form:form method="POST" action="/customer" commandName="customer">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <table>
            <tr>
                <td>Username :</td>
                <td><form:input path="userName" />
                </td>
                <td><form:errors path="userName" cssClass="error" />
                </td>
            </tr>
            <tr>
                <td colspan="3"><input type="submit" />
                </td>
            </tr>
        </table>
    </form:form>

</html>

当我使用简单的html表单然后它正常工作但同样的事情,如果我通过弹簧形式这样做它给出了错误。

控制器类:

@Controller
public class HomeController {


    @RequestMapping("/")
    public String welcomePage()
    {
        return "home";
    }


    @RequestMapping(value="/customer", method=RequestMethod.POST)
    public ModelAndView submitForm(@RequestParam("userName") String name)
    {       
        ModelAndView mv = new ModelAndView("success");
        mv.addObject("userName", name);
        return mv;
    }
}

6 个答案:

答案 0 :(得分:41)

您使用的是哪个版本的Spring MVC?我有同样的问题,maven依赖,

  

组ID:org.springframework   artifact id:spring-webmvc   版本:5.0.2.RELEASE

在版本5之后,删除了commandName,您应该使用modelAttribute。 我在这里找到了它,https://jira.spring.io/browse/SPR-16037

我只是用modelAttribute更改了commandName。

<form:form modelAttribute="goal">

答案 1 :(得分:1)

commandName =请求范围或会话范围中包含有关此表单信息的变量的名称,应该是。

参考文献:http://forum.spring.io/forum/spring-projects/web/59966-exact-meaning-of-form-form-commandname

Spring annotation relation with <form:form commandName="xy"

在显示(返回)带有表单的jsp文件的GET customer方法中,添加如下模型属性:

@RequestMapping(value="/customer", method=RequestMethod.GET)
    public ModelAndView showForm(Model model) {
        model.addAttribute("customer",new Customer());

}

然后尝试正确显示表单。

答案 2 :(得分:1)

1 ...旧方式= commandName

<form:form action="index" commandName="todo">

2 ... NEW WAY = modelAttribute

  <form:form action="index" modelAttribute="todo">

commandName是执行此操作的旧方法,在新应用程序中,您应该使用modelAttribute

答案 3 :(得分:0)

找不到属性的设置方法:[commandName] t

在此异常中,Spring Container想要知道,您在jsp表单标签中用作“ commandName”或“ modelAttribute”的名称,该属性属于哪个Domain(Class)。这意味着commandName="customer"客户是instanceOf客户模型类。还是其他的instance ..?为了确保Spring容器必须发送Model以便在welcomePage()方法的HomeController中进行查看,只需执行以下操作即可。

import org.springframework.ui.Model;
@Controller
public class HomeController {

    @RequestMapping("/")
    public String welcomePage(Model model)
    {
        /* i am not sure what would be youe Model name for customer change that accord to your model name */
        Customer customerInstance = new Customer();
        model.addAttribute("customer", customerInstance);
        return "home";
    }
}

Уou可以看到我在Model属性中添加了2个参数

  1. 属性名称,即客户
  2. 对象名称,即customerInstance

模型是接口,它是Spring API库的一部分,模型接口 简而言之,该模型可以提供用于渲染视图的属性。
为了向视图提供可用的数据,我们只需将此数据添加到其Model对象。此外,具有属性的地图可以与Model实例合并: If you want to know more go to this link

答案 4 :(得分:0)

在HomeController.java中,您需要显示具有相关模型的表单视图,如下所示

@RequestMapping(value="/customer", method=RequestMethod.GET)
    public ModelAndView showForm(Model model) {
        model.addAttribute("customer",new Customer());
}

,如果您使用的是spring-webmvc 5或最新版本,请确保将commandName="customer"替换为modelAttribute="customer"。 我希望这能解决您的问题。

答案 5 :(得分:-1)

可以通过在表单中​​将'commandName'更改为'modelAttribute'来解决。 对于Spring 5 jars,commandName参数不起作用。

请访问下面的链接,以获取有关Spring框架5中存在的更改的更多详细信息:

https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x