java - Action class value passing one method to another method -
the value not passing method 1 method 2 shows null pointer exception please me this.
in struts2 action class contains 2 methods. 1 method produce value used method. in jsp page have 2 buttons. 1 calls first method populate value. button calls second method in same class , use populated value first method , display values in jsp. every button click action class object destroyed , create new object every button click. first method values lost , can not referred second method. please tell me solution this.
struts2 action class :
public class crudaction{ private list<string> list; public list<string> getlist() { return list; } //method 1 public string one(){ list=new arraylist<string>(); list.add("one"); list.add("two"); return "success"; } //method 2 public string two(){ list.add("three"); return "success"; } }
struts.xml :
<action name="one" class="com.crudaction" method="one"> <result name="success">/index.jsp</result> </action> // second method <action name="two" class="com.crudaction" method="two"> <result name="success">/index.jsp</result> </action>
in jsp :
for first method
<input type = "button" value = "one" onclick = "javascript:location.href='one.action';"/>
second method
<input type = "button" value = "two" onclick"javascript:location.href='two.action';"/>
considering http protocol stateless object create in first request cannot referenced in second request hence there 2 options.
1 .create hidden input tag contain list id, when request second time, struts 2 method can id (as parameter). expensive create again depends on needs
2 .before render jsp in first request, can put object in session scope using actioncontext. eg
map<string,object> session = actioncontext.getcontext().getsession(); session.put("mylist",list); in second request can access object map<string,object> session = actioncontext.getcontext().getsession(); list<string> mylist=(list)session.get("mylist"); map<string,object> session = actioncontext.getcontext().getsession(); session.remove("mylist");
Comments
Post a Comment