Search code examples
databasejspstruts2ognl

Getting the database in struts2


i've done a struts2 login action using mysql database which is as shown below. for the initial login action i'm using LoginActionBean where i checks the username and password. After loging into the Main page there i need to get the team details again from the database. For that i used jsp scriplet tags. Can anyone tell me another way in which to access the team details from the database through javabeans (not through LoginActionBean) and without using the jsp scriplets.

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;
    }

}

Solution

  • Well you have already done most of the work, so you can do some simple things like in you action class

    1. Create a bean say TeamDetails with properties you want to show in JSP
    2. define this bean in your Action class and fill it using DB call
    3. use OGN in your JSP to access data and show it using struts2 tag.

    Action class:

    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
    }
    

    on your JSP using OGNL iterator tag to show the data

    JSP page

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