javaguy
javaguy

Reputation: 1133

how to access bean inside action in struts2

I have following test.jsp where i click on submit button and i am trying to set value in transactionBean in action and get it displayed on HelloWorld.jsp, but getTransactionBean() is returning null. Could you please let me know what am doing wrong.

test.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="displayActionlogmetoo.action" method="post">
    <s:submit method="logmetoo" key="login" align="center" />
</s:form>
</body>
</html> 

HelloWorld.jsp

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

   username <s:property value="transactionBean.username"/>
   password <s:property value="transactionBean.password"/>

</body>
</html>

My Struts.xml is:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>


<package name="default" namespace="/" extends="struts-default"> 
<action name="displayActionlogmetoo" 
    class="com.tutorialspoint.struts2.DisplayLoginAction" 
    method="logmetoo">

<result name="success">HelloWorld.jsp</result>
</action>
</package>
</struts>

My Action is:

public class DisplayLoginAction extends ActionSupport {

   private TransactionBean transactionBean;
   public TransactionBean getTransactionBean() {
        return transactionBean;
   }

   public void setTransactionBean(TransactionBean transactionBean) {
        this.transactionBean = transactionBean;
   }

   public String logmetoo(){
        System.out.println("Inside logmetoo");
        getTransactionBean().setUsername("usename");
        getTransactionBean().setPassword("password");
        return SUCCESS;
   }
}

Upvotes: 1

Views: 2063

Answers (1)

user1884155
user1884155

Reputation: 3736

There's two way of getting transactionBean instantiated:

1) Do it yourself. This is really simple:

public String logmetoo(){
    System.out.println("Inside logmetoo");

    setTransactionBean(new TransactionBean());
    getTransactionBean().setUsername("gaurav");
    getTransactionBean().setPassword("bhardwaj");
    return Action.SUCCESS;
}

2) Add input fields in your form and use the name attribute in to tell your action class it has to instantiate a field.

<s:form action="displayActionlogmetoo.action" method="post">
    <input type="text" name="transactionBean.userName"/>
    <input type="text" name="transactionBean.password"/>
    <s:submit method="logmetoo" key="login" align="center" />
</s:form>

By submitting a form to the server, struts will automatically see

name="transactionBean.userName"

and execute the following code:

displayLoginAction.setTransactionBean(new TransactionBean());
displayLoginAction.getTransactionBean().setUserName(*whatever is filled in in the input*);

This is not magic, it is done by the param interceptor automatically for you. The param interceptor is part of the default-stack of interceptors.

Upvotes: 1

Related Questions