Reputation: 61
I want to set the JSP variable value to the Struts tag
<s:set var="" value="">
in the following code I have to set the value of carbo
variable in <s:set name="c" value=" "/>
instead of constant value.
How can I do this?
<%!
String carbo="";
%>
<%
String dish_name=(String)session.getAttribute("d");
String calories_qty=(String)session.getAttribute("a");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/medical","root","root");
//Connection conn=dbConn.getConnection();
Statement stmt = conn.createStatement();
System.out.println("jsp dish_name="+dish_name);
System.out.println("jsp dish_name="+calories_qty);
ResultSet rs = stmt.executeQuery("Select * from calories c,dishdetail d where c.dishdetail_Id=d.id and d.dishName='"+dish_name+"' AND d.size='"+calories_qty+"' ");
while(rs.next()){
carbo=rs.getString("carbo");
//carbo=Float.parseFloat(rs.getString("carbo"));
}
System.out.println("jsp carbo:"+carbo);
}
catch(Exception e){
System.out.println("error:"+e);
}
%>
<s:set name="c" value="45" />
<p> carbo: <s:property value="#c" /></p>
<sj:progressbar cssStyle="width:20%; height:10px;" value="%{c}" onCompleteTopics="reloadfifthlist" onChangeTopics="mychangetopic"/>
Upvotes: 2
Views: 10484
Reputation: 1
The most easiest way to do it
<s:set var="c"><%=carbo%></s:set>
<s:property value="#c" />
But, instead of writing application logic in the scriptlets you should do it in the action, where you could place these variables right in the action class. The action could return dispatching result to the JSP. Then after creating getters and setters to the attributes you could use OGNL expression to reference the action attributes. For example
@Action(value="name", results={
@Result(location = "/path/to/page.jsp")
})
class MyAction extends ActionSupport {
private String carbo;
//getters and setters here
public String execute(){
//your logic here
return SUCCESS;
}
}
then all you need just reference this action attribute
<s:property value="%{carbo}" />
Upvotes: 1
Reputation: 2307
I hope I understand well what you try to acheive but I'd go with
<s:set name="c" value="<%= carbo %>"/>
Upvotes: 1