2012-08-30 87 views
-1

我已經完成了使用mysql數據庫的struts2登錄操作,如下所示。 爲我使用LoginActionBean在我檢查用戶名和密碼的初始登錄操作。登錄到主頁面後,我需要再次從數據庫中獲取團隊詳細信息。爲此,我使用了jsp scriplet標籤。任何人都可以告訴我通過javabeans(而不是通過LoginActionBean)並且不使用jsp腳本從數據庫訪問團隊詳細信息的另一種方式。在struts2中獲取數據庫

的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<head> 
</head> 
<body> 
    <s:form action="log"> 
    <s:textfield label="USERNAME" name="uname"/> 
    <s:password label="PASSWORD" name="pass"/> 
    <s:submit label="SUBMIT"/> 
    </s:form> 
</body> 
</html> 

main.jsp中

<%@page import="java.sql.ResultSet"%> 
<%@page import="DbCon.DataConnection"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Welcome to Employee Home!</h1> 
     SELECT A TEAM:<select name="team"> 
      <% 
      DataConnection db=new DataConnection(); 
      ResultSet rs=db.exeQuery("select * from team"); 
      while(rs.next()) 
       { 
      %> 
      <option value="<%=rs.getString("teamname")%>" ><%=rs.getString("teamname")%></option> 
      <% 
      } 
      %> 
     </select> 
    </body> 
</html> 

struts.xml中

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts> 
    <constant name="struts.devMode" value="true" /> 
    <package name="default" extends="struts-default"> 
     <action name="log" class="login.Action.LoginActionBean" > 
     <result name="success">/Main.jsp</result> 
     <result name="error">/login.jsp</result> 
     </action> 
    </package> 
</struts> 

DataConnection.java

package DbCon; 
import java.io.FileInputStream; 
import java.sql.*; 
public class DataConnection 
{ 
    Connection con; 
    Statement stmt; 
    PreparedStatement pstmt; 
    ResultSet rs=null; 
    public DataConnection() 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      con=DriverManager.getConnection("jdbc:mysql://localhost/test","root",""); 
      stmt=con.createStatement(); 
      con.setAutoCommit(false); 
     }catch(Exception e){ 
      System.out.println("Err in constructor"+e); 
     } 
    } 
    public ResultSet exeQuery(String query) 
    { 
     try 
     { 
      // con.commit(); 
      rs=stmt.executeQuery(query); 
     }catch(Exception e){System.out.println(e);} 
     return rs; 
    } 
    public int exeUpdate(String query) 
    { 
     int i = 0; 
     try 
     { 
      //con.commit(); 
      i=stmt.executeUpdate(query); 
      con.commit(); 
     }catch(Exception e){System.out.println(e);} 
     return i; 
    } 
} 

LoginActionBean.java

package login.Action; 

import DbCon.DataConnection; 
import com.opensymphony.xwork2.ActionSupport; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class LoginActionBean extends ActionSupport { 
public String uname,pass; 

    public String getPass() { 
     return pass; 
    } 

    public void setPass(String pass) { 
     this.pass = pass; 
    } 

    public String getUname() { 
     return uname; 
    } 

    public void setUname(String uname) { 
     this.uname = uname; 
    } 

    public String execute() throws SQLException 
    { 
     DataConnection db=new DataConnection(); 
     ResultSet rs=db.exeQuery("select * from admin where name='"+uname+"' and pass='"+pass+"'"); 
     if(rs.next()) 
     return SUCCESS; 
     else 
     return ERROR; 
    } 

} 

回答

0

那麼你已經完成了大部分的工作,這樣你就可以在你的動作類做一些簡單的事情,像

  1. 創建一個bean,說明你想在JSP中顯示的屬性的TeamDetails
  2. 在Action類中定義這個bean,並使用DB調用填充它
  3. 在JSP中使用OGN來訪問數據並使用struts2標籤顯示它。

Action類:

public class TeamDetailAction extends ActionSupport{ 
    List<TeamDetailData> teamDetails; 
    //getter and setter 

public String execute() throws Exception{ 
      teamDetails=fill it by retrieving from BD 
    } 
} 

TeamDetailData的Java

public class TeamDetailData{ 

private teamMemberName; 
// other properties 
//getter and setters 
} 

你的JSP使用OGNL iterator標籤來顯示數據

JSP頁面

<s:iterator value="teamDetails"> 
    <p>Name : <s:property value="teamMemberName"/></p> 
</s:iterator> 
+0

先生有兩類,即TeamDetailData和TeamDetailAction是必不可少的? –

+0

井操作類將處理您的操作,而TeamDetailData則像POJO一樣爲您保留團隊數據。 –

+0

仍然沒有輸出! –