如何使用struts1在jsp上显示数据库中的数据?

时间:2012-05-14 06:49:47

标签: struts1

我是struts的新手。我想在jsp上向用户显示数据库中的表中的所有细节。请帮我做这些。

提前谢谢, 维迪亚

LoginFormBean.java

public class LoginFormBean extends ValidatorForm 
{

    public String name;
    public String city;
    public String state;
    public String phone;
    public String mailId;
    public String gender;
    public String branch;
    private List studentDetails;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void setMailId(String mailId) {
        this.mailId = mailId;
    }

    public String getMailId() {
        return mailId;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getGender() {
        return gender;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }

    public List getStudentDetails() {
        return studentDetails;
    }
    public void setStudentDetails(List studentDetails) {
        this.studentDetails = studentDetails;
    }


}

Logindao.java

public class Logindao {
public List viewStudents()
    {

          Connection conn=DBUtil.getDBConnection();
          List list=new ArrayList();
          Statement stmt = null;
          ResultSet rs=null;
          String query="select * from student_info";

               stmt=conn.createStatement();
               rs= stmt.executeQuery(query);
              while(rs.next())
              {

                    list = new ArrayList();  
                    list.add(rs.getString("name"));
                    list.add(rs.getString("phone"));
                    list.add(rs.getString("email"));
                    list.add(rs.getString("city"));
                    list.add(rs.getString("state"));
                    list.add(rs.getString("gender"));
                    list.add(rs.getString("branch"));
              }

          }
              return list;
    }

RetrieveDataActionClass.java

public class RetrieveDataActionClass extends Action{

    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception,ServletException
     {
            List list=new ArrayList();
            String key="success";
            Logindao dao=new Logindao();
            LoginFormBean formbean=(LoginFormBean)form;

    try{

        list=dao.viewStudents();
        formbean.setStudentDetails(dao.viewStudents());
        }
        catch(Exception e)
        {
           e.printStackTrace();
           System.out.println("Error occured");

        }
        return mapping.findForward(key);
        }
        } 

viewstudents.jsp

<body>
   <html:form action="/retrieve" method="post">
    <table border="1">  
    <tr>
    <th>Name</th>
    <th>Phone</th>
    <th>MailId</th>
    <th>City</th>
    <th>State</th>
    <th>Gender</th>
    <th>Branch</th>
    </tr>  

    <logic:iterate id="students" name="LoginFormBean" property="studentDetails">
    <tr>  
    <td><bean:write name="students" property="name"/></td>  
    <td><bean:write name="students" property="phone"/></td>  
    <td><bean:write name="students" property="mailId"/></td>  
    <td><bean:write name="students" property="city"/></td>  
    <td><bean:write name="students" property="state"/></td>  
    <td><bean:write name="students" property="gender"/></td>  
    <td><bean:write name="students" property="branch"/></td>  
    </tr>  
    </logic:iterate> 
    </table>
    </html:form>
  </body>

我的sqlserver数据库中有一个表' student_info '。 列:姓名,电话,电子邮件,城市,州,性别,分支

我想在jsp中以以下格式显示输出。

姓名电话邮件城市州性别分支

abc 55555 a@gmail.com hyd AP女性IT

xyz 55555 x@gmail.com bang KT女CSE

错误:

javax.servlet.ServletException:javax.servlet.jsp.JspException:在任何范围内都找不到bean:“LoginFormBean”

先谢谢, 维迪亚。

1 个答案:

答案 0 :(得分:2)

您需要在逻辑:iterate之前在JSP中实例化您的bean“LoginFormBean”。你可以使用jsp:usebean标签来实现