Arthur
Arthur

Reputation: 3807

Struts 2 - Set bean on action

I'm trying to set a simple bean through a form with Struts 2 actions. I'm quite new with this framework, and I don't see what I'm missing...

The 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;
    }   
}

The action 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;
    }
}

The 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>

The result 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>

When I submit the form, my data isn't displayed and I get this exeption :

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)
[...]

Upvotes: 0

Views: 5299

Answers (1)

Chintan Dhingra
Chintan Dhingra

Reputation: 41

Hey I have just copied your code and make a dispatcher servlet, its working fine on my end. And no need to initialize FormBean in Action Class. it's correct.

Upvotes: 1

Related Questions