简体   繁体   English

如何将 JSP 变量值传递给这个 Struts 标签

[英]How to pass JSP variable value to this Struts tag

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.我想将 JSP 变量值设置为 struts 标签<s:set var="" value=""> ,在下面的代码中我必须在<s:set name="c" value=" "/> carbo变量的<s:set name="c" 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"/> 

我希望我能很好地理解你想要达到的目标,但我愿意

<s:set name="c" value="<%= carbo %>"/>

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.但是,不是在 scriptlet 中编写应用程序逻辑,您应该在操作中完成,您可以将这些变量直接放在操作类中。 The action could return dispatching result to the JSP.该动作可以将调度结果返回给 JSP。 Then after creating getters and setters to the attributes you could use OGNL expression to reference the action attributes.然后在为属性创建 getter 和 setter 之后,您可以使用 OGNL 表达式来引用操作属性。 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}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM