How are private fields available on jsp page in struts 2.x?

Hi everyone, I am working with struts 2.x and very new to this framework. I am following the list of guides and steps and my application is working correctly, but there is one thing that is not clear to me. How and when I declare one private field in my action class and using struts tag, an accessible private field in the jsp page. So, how does this happen, which is not clear to me:


<?xml version="1.0" encoding="UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <package name="default" namespace="/home" extends="struts-default">

        <action name="message" class="">
            <result name="hello">/hello.jsp</result>
            <result name="bye">/bye.jsp</result>
        <action name="add" class="">
            <result name="success">/hello.jsp</result>
            <result name="fail">/bye.jsp</result>




public class MessageAction {

    private String result;

    private String value;

    private String fstvalue;

     private String scndvalue;
    public String getFstvalue() {
        return fstvalue;

    public void setFstvalue(String fstvalue) {
        this.fstvalue = fstvalue;

    public String getScndvalue() {
        return scndvalue;

    public void setScndvalue(String scndvalue) {
        this.scndvalue = scndvalue;

    // method which get value from input parameter
    public String getValue() {
        return value;

    // method which show the value of input parameter
    public void setValue(String value) {
        this.value = value;

    // method which run as default and execute
    public String execute() {

        BussniessServ serv = new BussniessServ();
        setMessag(serv.Addition(fstvalue, scndvalue));
        return "hello";

    // method to show message on jsp page
    public String getResult() {

        return result;

    // method for save message
    public void setResult(String result) {
        this.result = result;




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<s:property value="result"/>



source to share

2 answers

Struts uses reflection to find the method that returns this variable. For example, if you have a variable named result

, Struts looks for a method named getResult()

and calls it to get the value.

Also, if you want the properties to be available in the JSP, you must make sure that you name these methods correctly. A property called "abc" will match the getAbc () method (if it is not boolean, in which case it will match the isAbc () method).



OGNL does not actually require accessors, although this is version dependent: early versions (~ S2.0) required accessor methods. Later versions (~ S2.1 +) have "helped" this limitation.

Public properties with private getters also work, although this is clearly pathological.

IMO this was a step backwards for data encapsulation.

It can be configured in later / current versions of OGNL, I'm not sure.



All Articles