[STRUTS] Probl鑝s mapping
Hello,
I try to do a simple struts application with struts v1.2.7. This application begins with an identification form (user + password). When a user try to connect on the application, a query is sent to the database. If the user is registered the user is redirected on the main page, named employeliste.jsp. If not the user is redirected on the identification page and errors are screened on it.
My problem is :
When i enter a good name user and a good password, there is a failure because i'm redirected on a white page whitout code, errors or logs...
I tried to see where the problem is and i saw that the authentification process is OK, because the mapping.findForward of my class LoginAction.java give an answer.
Here you will find :
struts-config.xml :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources>
<data-source type="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
<set-property property="driverClass" value="com.mysql.jdbc.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/employes" />
<set-property property="maxCount" value="5" />
<set-property property="maxCount" value="1" />
<set-property property="user" value="root" />
<set-property property="password" value="*****" />
</data-source>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="com.monAppli.LoginForm" />
</form-beans>
<global-forwards>
<forward name="login" path="/login.jsp"/>
</global-forwards>
<action-mappings>
<action path="/Login"
type="com.monAppli.LoginAction"
name="loginForm"
validate="true"
input="/login.jsp"
scope="request"
>
<forward name="succes" path="/EmployeListe.do"/>
</action>
<action path="/EmployeListe"
type="com.monAppli.EmployeListeAction"
scope="request"
className="com.monAppli.EmployesActionMapping"
>
<set-property property="loginRequired" value="true"/>
<forward name="succes" path="/employeliste.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.monAppli.ApplicationResources"/>
</struts-config>
LoginAction.java :
package com.monAppli;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
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;
publicclass LoginActionextends Action{
protected String getUser(String username,String password){
String user =null;
Connection conn =null;
Statement stmt =null;
ResultSet rs =null;
DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(Globals.DATA_SOURCE_KEY);
try{
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from employes where username='"+username+"' and password='"+password+"'");
if(rs.next()){
user = rs.getString("username");
//Iteration sur le resultat
System.err.println("Username : "
+ rs.getString("username")
+" Password : " + rs.getString("password"));
}else{
System.err.println("-->Utilisateur non trouv?lt;--");
}
}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 user;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
String user =null;
//Cible par d閒aut en cas de succ鑣
String target =new String("succes");
//Utilisation de LoginForm pour obtenir les param鑤res de la Qry
String username = ((LoginForm)form).getUserName();
String password = ((LoginForm)form).getPassword();
user = getUser(username, password);
//Cible en cas d'echec
if (user ==null){
target =new String("login");
ActionErrors errors =new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("errors.login.unknown",
username));
//Enregister les erreurs trouv閑s dans le formulaire original
if (!errors.isEmpty()){
saveErrors(request, errors);
}
}else{
HttpSession session =request.getSession();
session.setAttribute("USER", user);
System.err.println(" Username plac?dans session : "+session.getAttribute("USER")+"");
}
//Transmission de la requ阾e ?la vue appropri閑
System.err.println(" Login Target : "+target+"");
return (mapping.findForward(target));
}
}
EmployeListeAction
package com.monAppli;
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.Globals;
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;
public class EmployeListeAction extends Action{
protected ArrayList getEmployes(){
Employe employe = null;
ArrayList employes = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute(Globals.DATA_SOURCE_KEY);
try{
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from employes, roles, "
+ "services where employes.roleid=roles.roleid "
+ "and employes.depid = services.depid");
while (rs.next()){
employe = new Employe();
employe.setUserName(rs.getString("username"));
employe.setName(rs.getString("name"));
employe.setRolename(rs.getString("rolename"));
employe.setPhone(rs.getString("phone"));
employe.setEmail(rs.getString("email"));
employe.setRoleid(new Integer(rs.getInt("roleid")));
employe.setDepid(new Integer(rs.getInt("depid")));
employe.setDepartment(rs.getString("depname"));
employes.add(employe);
}
}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 employes;
}
public ActionForward execute(ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
//Cible par d閒aut en cas de succ閟
String target = new String("succes");
EmployesActionMapping employesMapping = (EmployesActionMapping)mapping;
HttpSession session = request.getSession();
System.err.println(" USER : -"+session.getAttribute("USER")+"-");
//Cette action necessite elle l'identification de l'utilisateur ?
if (employesMapping.isLoginRequired()){
if(session.getAttribute("USER") == null){
//L'utilisateur n'est pas identifi?
target = new String("login");
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.login.required"));
//Signaler les erreurs 関entuelles au formulaire original
if(!errors.isEmpty()){
saveErrors(request, errors);
}
}
}
ArrayList employes = null;
employes = getEmployes();
//Cible en cas d'echec
if (employes == null){
target = new String("login");
}else{
request.setAttribute("employes", employes);
}
//Forward to the appropriate view
System.err.println(" Employes Target : -"+target+"-");
return (mapping.findForward(target));
}
}
My custom mapping EmployesActionMapping :
package com.monAppli;
import org.apache.struts.action.ActionMapping;
publicclass EmployesActionMappingextends ActionMapping{
protectedboolean loginRequired =false;
publicvoid setLoginRequired(boolean loginRequired){
this.loginRequired = loginRequired;
}
publicboolean isLoginRequired(){
return loginRequired;
}
}
My JSP page who doesn't show me a result(employeliste.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" cellspacing="0" cellpadding="0">
<tr>
<td colspan="7"> </td>
</tr>
<tr bgcolor="#000000">
<td colspan="7" height="68" width="48%">
<div align="left">
<img src="images/hp_logo_monAppli.gif"
width="220" height="74">
</div>
</td>
</tr>
<tr>
<td colspan="7"> </td>
</tr>
</table>
<html:errors />
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr align="left">
<th><bean:message key="app.username" /></th>
<th><bean:message key="app.name" /></th>
<th><bean:message key="app.phone" /></th>
<th><bean:message key="app.email" /></th>
<th><bean:message key="app.department" /></th>
<th><bean:message key="app.role" /></th>
</tr>
<!--iteration sur les r閟ultat de la requete -->
<logic:iterate id="employe" name="employes">
<tr align="left">
<td>
<bean:write name="employe" property="username" />
</td>
<td>
<bean:write name="employe" property="name" />
</td>
<td>
<bean:write name="employe" property="phone" />
</td>
<td>
<bean:write name="employe" property="email" />
</td>
<td>
<bean:write name="employe" property="department" />
</td>
<td>
<bean:write name="employe" property="rolename" />
</td>
<td>
<a href="Edit.do?username=<bean:write name='employe' property='username' />">Modifier</a>
<a href="Delete.do?username=<bean:write name='employe' property='username' />">Supprimer</a>
</td>
</tr>
</logic:iterate>
<tr>
<td colspan="7"><hr></td>
</tr>
</table>
<font size="-1" face="arial">
<a href="addemploye.jsp">Ajouter un employ?lt;/a>
</font>
</body>
</html>
My web.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC"-//SUN Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>mapping</param-name>
<param-value>com.eyrolles.EmployesActionMapping</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>

