How to pass value from JSP to Struts java code dynamically -
How to pass value from JSP to Struts java code dynamically -
i working on struts. have jsp login page through user submit data. after if want search info have come in id
no. want id
entered dynamically. id
no passed java page. using jdbc. can 1 help me how write query , set string function. here java code:
// bundle com.javatpoint; import java.sql.*; import java.util.arraylist; public class display { arraylist<user> list=new arraylist<user>(); user user= new user(); public arraylist<user> getlist() { homecoming list; } public void setlist(arraylist<user> list) { this.list = list; } public string execute(){ try{ class.forname("com.mysql.jdbc.driver"); connection con=drivermanager.getconnection( "jdbc:mysql://localhost:3306/test","root","ericsson@123"); preparedstatement ps=con.preparestatement("select * strutsuser id=?"); ps.setint(0,user.getid()); resultset rs=ps.executequery(); while(rs.next()){ user user=new user(); user.setid(rs.getint(1)); user.setname(rs.getstring(2)); user.setpassword(rs.getstring(3)); user.setemail(rs.getstring(4)); list.add(user); } }catch(exception e){e.printstacktrace();} homecoming "succes"; } }
struts.xml
:
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="anbc" namespace="/" extends="struts-default"> <action name="display" class="com.javatpoint.display"> <result name="success">display.jsp</result> </action> </package> <package name="abc" namespace="/" extends="struts-default"> <action name="login" class="com.javatpoint.register"> <result name="success">welcome.jsp</result> </action> </package> </struts>
if utilize user
object model, not utilize model driven action, user id
should utilize user.id
parameter in request. should utilize getter model
public user getuser(){ homecoming user; }
when action called set parameter user object. set parameter in query
ps.setint(1, user.getid()); //parameters counted 1..n
see details of using preparedstatement
.
you should homecoming action.success
result, , there's no need name result "success"
because it's default name.
return action.success;
if passing user id parameter won't more 1 object in list, bind list in jsp can utilize iterator
tag
<s:iterator value="list"> id: <s:property value="id"/><br> name: <s:property value="name"/><br> email: <s:property value="email"/><br> </s:iterator>
the properties should have corresponding getters in user
object.
loading classes , opening connection (that forgot close) in action bad. connection
object can configure , utilize info source. see examples how jsf inegrated jdbc , how create mysql database connection in web.xml
of apache tomcat using struts 2.
java jsp jdbc struts2 ognl
Comments
Post a Comment