0

我正在尝试通过带有 Struts 2 操作的表单设置一个简单的 bean。我对这个框架很陌生,我看不出我错过了什么......

豆子FormBean

public class FormBean {
    private String login="";
    private String password="";

    public String getLogin() {
        return login;
    }
    public void setLogin(String login) {
        this.login = login;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }   
}

行动FormAction

public class FormAction extends ActionSupport {

    private FormBean form;  

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public FormBean getForm() {
        return form;
    }    
    public void setForm(FormBean form) {
        this.form = form;
    }
}

表格index.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head> 
<body>
<s:form action="login.action" method="post">
    <s:textfield name="form.login" label="User" size="20" />
    <s:password name="form.password" label="Password" size="20" />
    <s:submit method="execute" value="Login" align="center" />
</s:form>
</body>
</html>

结果Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>

<body>
    <h2>Welcome <s:property value="form.login" /></h2>
    <p><s:property value="form.password" /></p>
</body>
</html>

当我提交表单时,我的数据没有显示,我得到了这个例外:

Unexpected Exception caught setting 'login' on 'class tuto.form.FormAction: Error setting expression 'login' with value ['test', ]
Error setting expression 'login' with value ['test', ] - [unknown location]
    at com.opensymphony.xwork2.ognl.OgnlValueStack.handleRuntimeException(OgnlValueStack.java:197)
[...]
Caused by: No object in the CompoundRoot has a publicly accessible property named 'login' (no setter could be found). - [unknown location]
    at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:106)
[...]
4

1 回答 1

1

嘿,我刚刚复制了您的代码并制作了一个调度程序 servlet,它对我来说工作正常。并且不需要在 Action Class 中初始化 FormBean。这是正确的。

于 2013-10-03T09:06:43.923 回答