IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Programmation Application struts 1.3.10

Ce tutoriel contient seulement des images et le code source des pages utilisés dans l'application.

Chaque remarque sur la méthode du tutoriel est la bienvenue.

Vous pouvez me contacter sur jeha@soouknet.com

Article lu   fois.

L'auteur

Profil Pro

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

I. Étape 1 La plateforme utilisée est Eclipse

Image non disponible

II. Étape 2 New projet web StrutsJehaAplication

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

III. Étape 3 Chercher les lib 1.3.10

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

IV. Étape 4 Les pages de l'application

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

Les tld

Image non disponible
Image non disponible
Image non disponible

V. Étape 5 Les fichiers de configuration

Web.xml
Sélectionnez
<?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 id="WebApp_1238163974531"> 
<display-name>struts1</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</web-app>
struts-config.xml
Sélectionnez
<?xmlversion="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPEstruts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<!--
This is a blank Struts configuration file with an example
welcomeaction/page and other commented sample elements.
Tiles and the Struts Validatorare configured using the factory defaults
and are ready-to-use.
NOTE: If you have a generator tool to create the corresponding Java classes
for you, you could include the details in the "form-bean" declarations.
Otherwise, you would only define the "form-bean" element itself, with the
corresponding"name" and "type" attributes, as shown here.
-->
<struts-config>
<!--============================================ Data Source Configuration -->
<!--================================================ Form Bean Definitions -->
<form-beans>
<!-- la déclaration des beans de l'application -->
<form-bean name="UserLoginForm" 
type="jeha.web.struts.form.UserLoginForm">
</form-bean>
</form-beans>
<!--========================================= Global Exception Definitions -->
<global-exceptions>
<!-- sample exception handler
<exception
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample-->
</global-exceptions>
<!-- ===========================================Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "login" action -->
<!-- Demonstrates using index.jsp to forward -->
<!-- c'est une méthode de faire la redirection pour une page -->
<forward
name="login"
path="/Login.do"/>
</global-forwards>
<!--=========================================== Action Mapping Definitions -->
<action-mappings>
<!-- Default "Login" action-->
<!-- Forwards to userlogin.jsp-->
<action
path="/Login"
forward="/pages/userlogin.jsp"/>
<!-- ===============Declaration Action Mapping --> 
<!-- ===============User --> 
<action
path="/logout"
type="jeha.web.struts.action.Logout">
<forward name="success" path="/"/>
</action> 
<action
path="/userlogin"
name="UserLoginForm"
scope="request"
validate="true"
input="/pages/userlogin.jsp"
type="jeha.web.struts.action.UserLoginAction">
<forward name="success" path="/pages/loginsuccess.jsp"/>
<forward name="failure" path="/pages/userlogin.jsp"/>
</action>
</action-mappings>
<!--============================================= Controller Configuration -->
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<!--======================================== Message Resources Definitions -->
<message-resources parameter="MessageResources"/>
<!--=============================================== Plug Ins Configuration -->
<!-- ======================================================= Tiles plugin -->
<!--
This plugininitialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The pluginshould be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
latercase, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initializedplugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation
Paths found in Tiles definitions are relative to the main context.
-->
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<!-- Path to XML definition file-->
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml"/>
<!-- Set Module-awareness to true-->
<set-property property="moduleAware" value="true"/>
</plug-in>
<!-- =================================================== Validatorplugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>

VI. Étape 6 Les pages de l'application

Index.jsp
Sélectionnez
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<logic:redirect forward="login"/>
Top.jsp
Sélectionnez
<%@taglib uri="/tags/struts-bean" prefix="bean" %>

<%@taglib uri="/tags/struts-html" prefix="html" %>

<table border="0" width="100%" cellpadding="0" cellspacing="0">

<tr>

<td align="left" colspan="3" width="50%">

<p><fontsize="6" face="Monotype Corsiva"><a href="#"style="color: #6699CC">JEHA</a></font></p> </td>

</tr>

<tr>

<td align="center" bgcolor="#6699CC"width="25%">

</td>

<td bgcolor="#6699CC" align="center" width="25%">

</td>

<td width="25%" align="center"bgcolor="#6699CC">

</td>

<td width="25%" align="center"bgcolor="#6699CC">

<%if((String)session.getAttribute("userID") != null){%> <html:link page="/logout.do"><font color="white">Logout</font></html:link> <%}else{%>

<html:link page="/pages/userlogin.jsp"><font color="white">Login</font></html:link>

<%}%> </td>

</tr>

<tr> <td colspan="5">&nbsp;</td></tr>

</table>
loginsuccess.jsp
Sélectionnez
<%@taglib uri="/tags/struts-bean" prefix="bean" %>

<%@taglib uri="/tags/struts-html" prefix="html" %>

<%

String userid=(String)session.getAttribute("userID");

if(userid == null)

{

response.sendRedirect("../userlogin.jsp");

}else{

String id=(String)session.getAttribute("ID");

%> 

<html:html>

<head>

<title></title>

<html:base/>

</head>

<body>

<%@ include file="../top.jsp"%> 

<center>

<p><b>You are login successfuly !</b></p>

</center>

</body>

</html:html>

<%}%>
userlogin.jsp
Sélectionnez
<%@taglib uri="/tags/struts-bean" prefix="bean" %>

<%@taglib uri="/tags/struts-html" prefix="html" %>

<html:html >

<head>

<title><bean:message key="welcome.title"/></title>

<LINK rel="stylesheet"type="text/css" href="<html:rewrite page='/css/style.css'/>">

</head>

<html:base/>

<body>

<%@include file="../top.jsp"%> 

<center>

<table width="40%">

<tr>

<td>

<html:form action="/userlogin" method="post">

<table border="1" cellspacing="2" cellpadding="1" width="100%" class="signup">

<tr>

<td align="center"colspan="2" ><font size="5">User Login</font><br><font color="red"><html:errors/></font></td>

</tr> 

<!--display errors -->

<tr align="center">

<td align="right" width="50%"><b>User ID:</b></td> 

<td width="50%"><html:text property="userid" size="30" maxlength="30"/></td>

</tr> 

<tr align="center">

<td align="right"><b>Password:</b></td> 

<td><html:password property="password" size="30" maxlength="30"/></td>

</tr> 

<tr>

<td align="center"colspan="2"><html:submit>Sign-In !</html:submit></td>

</tr> 

</table>

</html:form>

</td>

</tr>

</table>

</center>

<body>

</html:html>

VII. Étape 7 Les packages de l'application

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

VIII. Étape 8 Le fichier des messages MessageResources.properties

Image non disponible
Image non disponible
Image non disponible
Image non disponible

IX. Étape 9 Les class de l'application

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

Logout.java

Image non disponible
 
Sélectionnez
package jeha.web.struts.action;

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;

public class Logout extends Action

{

public ActionForward execute(

ActionMappingmapping,

ActionFormform,

HttpServletRequestrequest,

HttpServletResponseresponse) throws Exception{

try{

HttpSession session = request.getSession(false);

if(session!=null) {

session.invalidate();

} 

}catch(Exception e){

StringstrError=e.getMessage();

System.out.println("Error is: " + strError);

}

return mapping.findForward("success");

}

}

UserLoginForm.java

Image non disponible
 
Sélectionnez
package jeha.web.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class UserLoginForm extends ActionForm{
private String action="add";
private String userid = null;
private String password = null;
public void reset(ActionMapping mapping,HttpServletRequestrequest){
this.userid=null;
this.password=null; 
this.action="add";
}
public ActionErrors validate( 
ActionMapping mapping,HttpServletRequest request ){
ActionErrorserrors = new ActionErrors();
return errors;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}

UserLoginAction.java

Image non disponible
MessageResources.properties
Sélectionnez
package jeha.web.struts.action;
import jeha.web.struts.form.UserLoginForm;
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.ActionMessage;
import org.apache.struts.action.ActionMessages;
public class UserLoginAction extends Action{
public ActionForward execute(
ActionMappingmapping,
ActionForm form,
HttpServletRequestrequest,
HttpServletResponseresponse) throws Exception{
boolean loginStatus =false;
// Create object of ActionMesssages
ActionMessageserrors = new ActionMessages();
UserLoginFormobjForm = (UserLoginForm) form;
String strUserid=objForm.getUserid();
String strPassword=objForm.getPassword();
if(strUserid.equals("jeha") && strPassword.equals("123") ){
loginStatus = true;
}
if(loginStatus==true){
HttpSession session = request.getSession();
try{
}catch (Exception e) {
System.out.println("===> Error:" + e.getMessage());
}
session.setAttribute("userID", strUserid); 
return mapping.findForward("success");
} else {
// not allowed
errors.add("login",new ActionMessage("error.login.invalid"));
saveErrors(request,errors);
return mapping.findForward("failure");
}
}
}

MessageResources.properties

Image non disponible
 
Sélectionnez
welcome.title=Struts JEHA Application
welcome.heading=Welcome!
error.login.invalid=Invalid User Name Or Password !
Image non disponible

X. Étape 10 Exécuter l'application

Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible
Image non disponible

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Copyright © 2013 JEHA Radouane . Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.