如何在jsp页面中从动作类和视图发送值

时间:2014-10-21 12:14:23

标签: java jsp struts-1

 public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        PrintWriter out=response.getWriter();
         FindBean lb=(FindBean)form;
        String bldgrp=lb.getBldgrp();
        String city=lb.getCity();
        String locality=lb.getLocality();
        String state=lb.getState();
        
        try {
             Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/web","root","password");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select name,email,phone from register where bldgrp = '" + bldgrp + "'AND state ='" + state + "'AND city ='" + city + "'AND locality ='" + locality + "'");

while(rs.next())
{

}
            
   
        }
        catch(Exception e)
{
out.println(e.getMessage());
}

我希望存储在Resultset对象rs中的结果显示在下一个jsp页面上。我在netbeans ide中使用struts

3 个答案:

答案 0 :(得分:2)

将其作为HttpServletRequest的属性发送,请检查setAttribute方法

request.setAttribute("here_the_name",here_the_object);

在JSP页面中,您可以使用getAttribute方法

提取它
Object o = request.getAttribute("here_the_name");

您可以发送任何对象,只需要投射它。

答案 1 :(得分:2)

找到以下操作代码

ResultSet rs=stmt.executeQuery("select name,email,phone from register where bldgrp = '" + bldgrp + "'AND state ='" + state + "'AND city ='" + city + "'AND locality ='" + locality + "'");
Myobject obj = new MyObject();

while(rs.next()) {
    obj.setName(rs.getString(1));
    obj.setEmail(rs.getString(2));
    obj.setPhone(rs.getString(3));

}

request.setAttribute("myObj", obj);

在jsp中:

<logic:present name="myObj">
Name : <bean:write name="myObj" property="name"/>
Email : <bean:write name="myObj" property="email"/>
Phone : <bean:write name="myObj" property="phone"/>
</logic:present>

希望这可能有所帮助..

答案 2 :(得分:1)

HttpSession session = request.getSession();
session.setAttribute("myResultSet", rs);

在你jsp:

ResultSet rs = (ResultSet)session.getAttribute("myResultSet");

只需将对象存储在会话中并在jsp中检索即可。见上文。