aspose file tools*
The moose likes Struts and the fly likes struts 2 action on Jsp loa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 2 action on Jsp loa" Watch "struts 2 action on Jsp loa" New topic
Author

struts 2 action on Jsp loa

karthick rathnan
Greenhorn

Joined: Dec 10, 2010
Posts: 2
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

Joined: May 09, 2002
Posts: 437
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



Help gets you when you need it!
karthick rathnan
Greenhorn

Joined: Dec 10, 2010
Posts: 2
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

Joined: May 09, 2002
Posts: 437
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

Joined: May 09, 2002
Posts: 437
Sorry

getSavingList() and setSavingList(...)

and not

getsavingList() / setsavingList(...)

Thank you,

Maki Jav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts 2 action on Jsp loa