Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts 2 action on Jsp loa

 
karthick rathnan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can a action class can be called when a new jsp page is loaded.Once user logs in,a jsp page with prepulated table values needs to be displayed.
Table value's are taken from an list available in the action class and it needs to be called to called before the page loads
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
displaying Companies List in JSP as select

Company is a Bean with id and name as its fields ,along with getter/setters.




displaying Companies List in JSP as Table










In Action class

public class MyAction extends ActionSupport implements SessionAware {

private ArrayList companies;


public String execute() throws Exception {

return readDatabase();

}



private String readDatabase(){

// Database reading layer

companies = "set values from database";

return "some keyword";

}



public void setCompanies(ArrayList<Company> companies){

this.companies = companies;

}


public ArrayList<Company> getCompanies(){

return this.companies;

}



}



in struts.xml


// if putting in default package...

<package name="default" extends="struts-default">
<action name="login" class="MyAction">
<result name="some keyword">yourJSP.jsp</result>
</action>

</package>





Hope you understand the basic structure


your login jsp should redirect it to action class with some parameter so as to distinguish on which method to go if you have if condition in te execute method or no such need arise if your method is just calling a database method.


Maki Jav


 
karthick rathnan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my code,can you help me populate the savinglist values in test.jsp.test.jsp is loaded on success of login jsp but savings list is not populated on load of test.jsp

struts.xml

<package name="default" extends="struts-default" >
<action name="login" class="LoginAction">
<result name="success">test.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="savings" class="CalculationAction">
<result name="success">test.jsp</result>
<result name="error">Login.jsp</result>
</action>


test.jsp

<s:action name="savings"></s:action>
<table>
<s:iterator value="savingList">
<tr>
<TD> <s:property value="sno"/></TD></tr>
</s:iterator>
</table>

calculation action

public String execute() {
createsavingList();
System.out.println("Inside Execute");
//getsavingList();
return "success";
}

public void createsavingList(){
if(savingList ==null){
savingList=new ArrayList<InteriorCalc>();
//savingList()
System.out.println("there in null");
}
if(savingList.size()== 0){
InteriorCalc firstRow=new InteriorCalc();
firstRow.setsno(1);

savingList.add(firstRow);
System.out.println("Bean added in List ");
//setSession(arg0)
}

}

public ArrayList<InteriorCalc> getsavingList() {
return this.savingList;
}
public void setsavingList(ArrayList<InteriorCalc> savingList) {
this.savingList = savingList;
}
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry, I was very busy with my project,

don't see the class level field savingList declared anywhere.

Anyways,

savingList has getter setters

but these should be


getSavingList() and setsavingList(...)

and not

getsavingList() / setsavingList(...)

There you go. You problem is solved



Maki Jav
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry

getSavingList() and setSavingList(...)

and not

getsavingList() / setsavingList(...)

Thank you,

Maki Jav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic