user3180807
user3180807

Reputation: 11

Struts2 javabeans and setter

i'm writing a simple form to take name and surname

this is my index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form di inserimento</title>
</head>
<body>
    <h1>Inserisci il tuo nome e il tuo cognome</h1>
    <s:form action="Form">
        <s:textfield name="nome" label="il tuo nome"/>
        <s:textfield name="cognome" label="il tuo cognome"/>
        <s:submit/>
    </s:form>
</body>
</html>

this is my ResultForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Risultato della form</title>
</head>
<body>
    <h1><s:property value="customPage"/></h1>
</body>
</html>

and this is my Action class

package com.paolo;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Form extends ActionSupport {
    private static final String GREETINGS = "Congratulazioni ";
    private String nome;
    private String cognome;
    private String customPage;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void SetCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getCustomPage() {
        return customPage;
    }

    public void setCustomPage(String customPage) {
        this.customPage = customPage;
    }

    public String execute() {
        setCustomPage(GREETINGS + " " + getNome() + " " + getCognome());
        return SUCCESS;
    }

}

it's all ok but the cognome (= surname) is sets to null when i go in the ResultForm.jsp thanks for the help

Upvotes: 1

Views: 47

Answers (1)

Jorge_B
Jorge_B

Reputation: 9872

The name of the setter should be setCognome instead of SetCognome for struts2 to populate the bean correctly.

http://en.wikibooks.org/wiki/Java_Programming/JavaBeans

Upvotes: 2

Related Questions