-
Help! How do you...
Help!! Question: How do you call parameters from a ActionForm bean to fill
username variable in SearchFormAction?
I am trying to request the variable username from SearchForm.java to incorporate
into SearchFormAction.java and tie into my SQL query and diplay in showallrecords.jsp
It's sample code I am trying to Prototype and make generic for future use.
Anybody have a simple solution?????
Thanks,
Marc
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!--
Here we make the data layer avaliable to the application with use of a Datasource.
-->
<data-sources>
<data-source>
<set-property property="driverClass"
value="org.gjt.mm.mysql.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost/employees" />
<set-property property="maxCount"
value="5"/>
<set-property property="minCount"
value="1"/>
<set-property property="user"
value="username"/>
<set-property property="password"
value="password"/>
</data-source>
</data-sources>
<form-beans>
<form-bean name="loginForm"
type="com.fprb.LoginForm" />
<form-bean name="employeeForm"
type="com.fprb.EmployeeForm" />
<form-bean name="SearchForm"
type="com.fprb.SearchForm" />
<form-bean name="MarcForm"
type="com.fprb.MarcForm" />
</form-beans>
<global-forwards>
<forward name="login" path="/login.jsp"/>
<forward name="search" path="/searchrecord.jsp"/>
<forward name="marc" path="/marc.jsp"/>
</global-forwards>
<action-mappings>
<action path="/Login"
type="com.fprb.LoginAction"
validate="true"
input="/login.jsp"
name="loginForm"
scope="request" >
<forward name="success" path="/mainmenu.jsp"/>
<!--<forward name="success" path="/EmployeeList.do"/>-->
</action>
<action path="/marc"
type="com.fprb.MarcFormAction"
validate="true"
input="/marc.jsp"
name="MarcForm"
scope="request" >
<forward name="success" path="/MarcFormAction.do"/>
</action>
<action path="/MarcFormAction"
type="com.fprb.MarcFormAction"
scope="request" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/showMarc.jsp"/>
</action>
<action path="/SearchFormAction"
type="com.fprb.SearchFormAction"
scope="request" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/showallrecords.jsp"/>
</action>
<action path="/EmployeeList"
type="com.fprb.EmployeeListAction"
scope="request" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/showallrecords.jsp"/>
</action>
<action path="/Add"
type="com.fprb.AddEmployeeAction"
name="employeeForm"
scope="request"
input="/addrecord.jsp"
validate="true" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/EmployeeList.do"/>
<forward name="error" path="/addrecord.jsp"/>
</action>
<action path="/Edit"
type="com.fprb.GetEmployeeAction"
name="employeeForm"
scope="request"
validate="false" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/editrecord.jsp"/>
<forward name="error" path="/EmployeeList.do"/>
</action>
<action path="/EditEmployee"
type="com.fprb.EditEmployeeAction"
name="employeeForm"
scope="request"
input="/editrecord.jsp"
validate="true" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/EmployeeList.do"/>
<forward name="error" path="/editrecord.jsp"/>
</action>
<action path="/Delete"
type="com.fprb.DeleteEmployeeAction"
scope="request"
validate="false" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/EmployeeList.do"/>
</action>
<action path="/Search"
type="com.fprb.SearchFormAction"
name="SearchForm"
scope="request"
input="/searchrecord.jsp"
validate="true" >
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/SearchFormAction.do"/>
<forward name="error" path="/searchrecord.jsp"/>
</action>
</action-mappings>
</struts-config>
//searchform.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title><bean:message key="app.title" /></title>
</head>
<body>
<table width="500"
border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
<tr bgcolor="#36566E">
<td width="48%" height="68" bgcolor="#FFFFFF"> <div align="center"><img
src="images/logo.gif" width="200" height="50">
</div></td>
</tr>
<tr>
<td><div align="center">Search Records:</div></td>
</tr>
</table>
<html:errors />
<div align="center"><html:form action="/Search"
name="SearchForm"
type="com.fprb.SearchForm" >
<table width="500" border="1">
<tr>
<td width="166"><bean:message key="app.username" />:</td>
<td width="91"><html:text property="username" /></td>
<td width="128"><bean:message key="app.password" />:</td>
<td width="87"><html:text property="password" /></td>
</tr>
<tr>
<td><bean:message key="app.name" />:</td>
<td><html:text property="name" /></td>
<td><bean:message key="app.phone" />:</td>
<td><html:text property="phone" /></td>
</tr>
<tr>
<td><bean:message key="app.email" />:</td>
<td><html:text property="email" /></td>
<td><bean:message key="app.department" />:</td>
<td> <html:select property="depid" size="1"> <html ption value="1">
<bean:message key="app.administration" />
</html ption> <html ption value="2"> <bean:message key="app.network"
/>
</html ption> <html ption value="3"> <bean:message key="app.sales"
/>
</html ption> <html ption value="4"> <bean:message key="app.engineering"
/>
</html ption> </html:select> </td>
</tr>
<tr>
<td><bean:message key="app.role" />:</td>
<td> <html:select property="roleid" size="1"> <html ption value="1">
<bean:message key="app.manager" />
</html ption> <html ption value="2"> <bean:message key="app.employee"
/>
</html ption> </html:select> </td>
<td colspan="2" align="center"> <html:submit /><html:cancel /><html:reset
/>
</td>
</tr>
</table>
</html:form>
<p><img src="images/struts-power.gif" width="95" height="37"></p>
</div>
</body>
</html>
//SearchForm.java
package com.fprb;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
public class SearchForm extends ActionForm {
protected String username;
protected String password;
protected String name;
protected String phone;
protected String email;
protected String depid;
protected String roleid;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setDepid(String depid) {
this.depid = depid;
}
public String getDepid() {
return depid;
}
public void setRoleid(String roleid) {
this.roleid = roleid;
}
public String getRoleid() {
return roleid;
}
// This method is called with every request. It resets the Form
// attribute prior to setting the values in the new request.
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = "";
this.password = "";
this.name = "";
this.phone = "";
this.email = "";
this.depid = "1";
this.roleid = "1";
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
EmployeesActionMapping employeesMapping =
(EmployeesActionMapping)mapping;
// Does this action require the user to login
if ( employeesMapping.isLoginRequired() ) {
HttpSession session = request.getSession();
if ( session.getAttribute("USER") == null ) {
// return null to force action to handle login
// error
return null;
}
}
// if ( (roleid == null ) || (roleid.length() == 0) ) {
// errors.add("roleid", new ActionError("errors.roleid.required"));
// }
// if ( (depid == null ) || (depid.length() == 0) ) {
// errors.add("depid", new ActionError("errors.depid.required"));
// }
// if ( (email == null ) || (email.length() == 0) ) {
// errors.add("email", new ActionError("errors.email.required"));
// }
// if ( (phone == null ) || (phone.length() == 0) ) {
// errors.add("phone", new ActionError("errors.phone.required"));
// }
// if ( (name == null ) || (name.length() == 0) ) {
// errors.add("name", new ActionError("errors.name.required"));
// }
// if ( (password == null ) || (password.length() == 0) ) {
// errors.add("password", new ActionError("errors.password.required"));
// }
// if ( (username == null ) || (username.length() == 0) ) {
// errors.add("username", new ActionError("errors.username.required"));
// }
// return errors;
// }
//}
//SearchFormAction.java
package com.fprb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.ArrayList;
protected ArrayList getEmployees() {
String username = null;
//Q: How do you call parameters from a ActionForm bean to fill username variable?
Notice how it's tied to the sql query...
Employee employee = null;
ArrayList employees = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)
context.getAttribute(Action.DATA_SOURCE_KEY);
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs =
stmt.executeQuery("select * from employees where username=\'"
+ username + "' ");
while ( rs.next() ) {
employee = new Employee();
employee.setUsername(rs.getString("username"));
employee.setName(rs.getString("name"));
employee.setRolename(rs.getString("rolename"));
employee.setPhone(rs.getString("phone"));
employee.setEmail(rs.getString("email"));
employee.setRoleid(new Integer(rs.getInt("roleid")));
employee.setDepid(new Integer(rs.getInt("depid")));
employee.setDepartment(rs.getString("depname"));
employees.add(employee);
System.err.println("Username : "
+ employee.getUsername()
+ " Department : " + rs.getString("depname"));
}
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
conn = null;
}
}
return employees;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Default target to success
String target = new String("success");
EmployeesActionMapping employeesMapping =
(EmployeesActionMapping)mapping;
// Does this action require the user to login
if ( employeesMapping.isLoginRequired() ) {
HttpSession session = request.getSession();
if ( session.getAttribute("USER") == null ) {
// The user is not logged in
target = new String("login");
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("errors.login.required"));
// Report any errors we have discovered back to
// the original form
if (!errors.empty()) {
saveErrors(request, errors);
}
}
}
ArrayList employees = null;
employees = getEmployees();
// Set the target to failure
if ( employees == null ) {
target = new String("login");
}
else {
request.setAttribute("employees", employees);
}
// Forward to the appropriate View
return (mapping.findForward(target));
}
}
//showallrecords.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:notPresent name="USER">
<logic:forward name="login" />
</logic:notPresent>
<html>
<head>
<title><bean:message key="app.title" /></title>
</head>
<body>
<table width="650"
border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="7"> </td>
</tr>
<tr bgcolor="#36566E">
<td width="48%" height="68" colspan="7" bgcolor="#FFFFFF"> <div align="center"><img
src="images/logo.gif" width="200" height="50">
</div></td>
</tr>
<tr>
<td colspan="7"><div align="center">Display All Records:</div></td>
</tr>
</table>
<html:errors />
<table width="978"
border="1" align="center" cellpadding="2" cellspacing="2">
<tr align="left">
<th width="122" height="26"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.username" /></strong></font></th>
<th width="105"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.name" /></strong></font></th>
<th width="141"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.phone" /></strong></font></th>
<th width="176"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.email" /></strong></font></th>
<th width="146"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.department" /></strong></font></th>
<th width="146"><font size="3" face="Arial, Helvetica, sans-serif"><strong><bean:message
key="app.role" /></strong></font></th>
</tr>
<!-- iterate over the results of the query -->
<logic:iterate id="employee" name="employees">
<tr align="left">
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="username" />
</font></td>
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="name" />
</font></td>
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="phone" />
</font></td>
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="email" />
</font></td>
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="department" />
</font></td>
<td> <font size="2" face="Arial, Helvetica, sans-serif"><bean:write name="employee"
property="rolename" />
</font></td>
<td width="82"> <a href="Edit.do?username=<bean:write name="employee"
property="username" />">Edit</a> <a href="Delete.do?username=<bean:write
name="employee"
property="username" />">Delete</a> </td>
</tr>
</logic:iterate>
</table>
<div align="center">
<!--<p><font size="-1" face="arial"> <a href="addrecord.jsp">Add New Employee</a>
</font></p>-->
<p><font size="-1" face="arial"><img src="images/struts-power.gif" width="95"
height="37"></font>
</p>
</div>
</body>
</html>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks