-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;
}
}
先生有兩類,即TeamDetailData和TeamDetailAction是必不可少的? –
井操作類將處理您的操作,而TeamDetailData則像POJO一樣爲您保留團隊數據。 –
仍然沒有輸出! –