Академический Документы
Профессиональный Документы
Культура Документы
Technologie Web
Un framework J2EE : Struts
Alexandre Pauchet
INSA Rouen - Département ASI
BO.B.RC.18, pauchet@insa-rouen.fr
INSA - ASI TechnoWeb : Struts 2/36
Plan
1 Introduction
2 Fonctionnement
6 Conclusion
INSA - ASI TechnoWeb : Struts 3/36
Introduction (1/6)
Description
Introduction (2/6)
Le MVC (rappel)
Introduction (3/6)
Le MVC sans Struts
Des JavaBeans (et autres classes) qui gèrent l’aspect métier (modèle)
Des JSP (éventuellement sans code Java) qui produisent l’affichage
(les vues)
Une ou plusieurs Servlets font le lien entre les JavaBeans et les JSP,
via les mécanismes de Délégation/Inlusion (contrôleur(s))
INSA - ASI TechnoWeb : Struts 6/36
Fonctionnement (4/6)
L’idée de Struts
Introduction (5/6)
Installation
Introduction (6/6)
Contenu (partiel) de struts-blank
struts-blank.war
|_ META-INF
| |_ MANIFEST.MF
|_ pages
| |_ Welcome.jsp
|_ WEB_INF
| |_ classes
| |_ lib
| | |struts.jar, jakarta-oro.jar, ...
| |_ src
| |_ struts-config.xml, web.xml, ...
| |_ struts-html.tld, struts-beans.tld, ...
|_ index.jsp
INSA - ASI TechnoWeb : Struts 9/36
Fonctionnement (1/9)
Fonctionnement d’une application Struts
Fonctionnement (2/9)
La méthode execute
Fonctionnement (3/9)
Le fichier web.xml de Struts (extrait)
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
INSA - ASI TechnoWeb : Struts 12/36
Fonctionnement (4/9)
Exemple : login
Fonctionnement (5/9)
Exemple : le fichier login.html
index.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
< title >Page de login pour Struts 1</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<form action="login.do" method="POST">
<input type="text" name="login"/>
<input type="password" name="password"/>
<input type="submit"/>
</form>
</body>
</html>
INSA - ASI TechnoWeb : Struts 14/36
Fonctionnement (6/9)
Exemple : struts-config.xml
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/login" type="monAppli.LoginAction">
<forward name="succes" path="/pages/ok.jsp"/>
<forward name="echec" path="/pages/echec.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<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>
INSA - ASI TechnoWeb : Struts 15/36
Fonctionnement (7/9)
Exemple : LoginAction.java
LoginAction.java
package monAppli;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class LoginAction extends Action {
public ActionForward execute (ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String login = request.getParameter ("login");
String password = request.getParameter ("password");
if (login.equals(password))
return mapping.findForward("succes");
else
return mapping.findForward("echec");
}
}
INSA - ASI TechnoWeb : Struts 16/36
Fonctionnement (8/9)
Exemple : ok.jsp
ok.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
< title >Hello !</title>
<html:base/>
</head>
<body bgcolor="white">
<h1>Hello !!! Login/mdp bien trouvés...</h1>
</body>
</html:html>
INSA - ASI TechnoWeb : Struts 17/36
Fonctionnement (9/9)
Exemple : echec.jsp
echec.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
< title >Echec à la connexion !</title>
<html:base/>
</head>
<body bgcolor="white">
<h1>Echec à la connexion : mauvais login/mdp...</h1>
</body>
</html:html>
INSA - ASI TechnoWeb : Struts 18/36
struts-config.xml
<form-beans>
<form-bean name="logBean" type="monAppli.LogBean"/>
</form-beans>
<action-mappings>
<action path="/login" type="monAppli.LoginAction" name="logBean" scope="session">
<forward name="succes" path="/pages/ok.jsp"/>
<forward name="echec" path="/pages/echec.jsp"/>
</action>
</action-mappings>
INSA - ASI TechnoWeb : Struts 19/36
LogBean.java
package monAppli;
import org.apache.struts.action.*;
public class LogBean extends ActionForm {
private String login;
private String password;
public String getLogin(){
return this.login;
}
public void setLogin(String login){
this .login = login;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this .password = password;
}
}
INSA - ASI TechnoWeb : Struts 20/36
UserBean.java
package monAppli;
import java.io.*;
import org.apache.struts.action.*;
public class UserBean implements Serializable {
private String login;
private String name;
public String getLogin(){
return this.login;
}
public void setLogin(String login){
this .login = login;
}
public String getName(){
return this.name;
}
public void setName(String name){
this .name = name;
}
public static UserBean getUser(String login){
UserBean ub = new UserBean();
ub.setLogin(login);
ub.setName("Bob Leponge");
return ub;
}
}
INSA - ASI TechnoWeb : Struts 21/36
LoginAction.java
package monAppli;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class LoginAction extends Action {
public ActionForward execute (ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
LogBean bean = (LogBean)actionForm;
String login = bean.getLogin ();
String password = bean.getPassword ();
if (login.equals(password)) {
UserBean userBean = UserBean.getUser(login);
request.setAttribute("userBean", userBean);
return mapping.findForward("succes");
}
else
return mapping.findForward("echec");
}
}
INSA - ASI TechnoWeb : Struts 22/36
Utilisation de la taglib proposée pour la gestion des Beans (un peu plus
pratique que celle par défaut)
ok.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html locale="true">
<head>
< title >Hello !</title>
<html:base/>
</head>
<body bgcolor="white">
<h1>Salut <bean:write name="userBean" property="name"/> !</h1>
</body>
</html:html>
INSA - ASI TechnoWeb : Struts 23/36
On modifie struts-config.xml :
<action path="..." type="..." validate="true" input="/
erreurs.jsp">...</action>
erreur.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head><title>Erreurs dans les champs du formulaire</title><html:base/></head>
<body bgcolor="white">
<h1>Erreurs dans les champs du formulaire</h1>
<html:errors/>
</body>
</html:html>
LogBean.java
package monAppli;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class LogBean extends ActionForm {
private String login;
private String password;
public String getLogin(){
return this.login;
}
public void setLogin(String login){
this .login = login;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this .password = password;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (this.getLogin().equals(""))
errors.add("login", new ActionError("errors.login"));
return errors;
}
}
INSA - ASI TechnoWeb : Struts 26/36
MessageResources.properties
# -- standard errors --
errors.header=<font color="#FF0000"><ul>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</ul></font>
# -- validator --
errors.login=Il faut mettre un Login !
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
...
INSA - ASI TechnoWeb : Struts 27/36
Exemple
journees.war
|_ WEB_INF
| |_ src
| | |_ journees
| | | |_ LookupForm.java, LookupAction.java
| |_ classes
| | |_ journees
| | | |_ LookupForm.class, LookupAction.class
| | |_ MessageRessources.properties
| |_ lib
| | |_struts.jar, ...
| |_ struts-config.xml, web.xml, ...
| |_ struts-html.tld, struts-logic.tld, ...
|_ index.jsp, lieu.jsp, erreur.jsp
INSA - ASI TechnoWeb : Struts 28/36
LookupForm.java
package journees;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class LookupForm extends ActionForm {
private String theme = null;
static final long serialVersionUID=1L;
public String getTheme() {
return this.theme;
}
public void setTheme(String theme) {
this .theme = theme;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this .theme = null;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors erreurs = new ActionErrors();
if (this.getTheme().equals("")){
erreurs.add("lookup", new ActionError("errors.theme"));
}
return erreurs;
}
}
INSA - ASI TechnoWeb : Struts 29/36
LookupAction.java
package journees;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class LookupAction extends Action {
protected String getLieu(String theme) {
if ( theme.equalsIgnoreCase("TechnoWeb") )
return new String("Saint-Etienne du Rouvray");
else return null;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String lieu = null, cible = new String("succes");
if ( form != null )
lieu = getLieu(((LookupForm)form).getTheme());
if ( lieu == null ) {
cible = new String("echec");
} else request.setAttribute("LIEU", lieu);
return (mapping.findForward(cible));
}
}
INSA - ASI TechnoWeb : Struts 30/36
index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
< title >Journees thematiques</title>
</head>
<body>
<html:form action="Lookup.do" method="POST">
<p>Journees thematiques : <html:text property="theme" /></p>
<html:errors/>
<html:submit/>
</html:form>
</body>
</html>
INSA - ASI TechnoWeb : Struts 31/36
lieu.jsp
<html>
<head>
< title >Struts Application</title>
</head>
<body>
<h2>lieu : <%= request.getAttribute("LIEU") %></h2>
</body>
</html>
erreur.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
< title >Erreur de saisie</title>
</head>
<body>
<h1>Erreur de saisie</h1>
<html:link href="/struts-journees/index.jsp">retour a la page de saisie</html:link>
</body>
</html>
INSA - ASI TechnoWeb : Struts 32/36
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
<form-beans>
<form-bean name="lookupForm" type="journees.LookupForm"/>
</form-beans>
<action-mappings>
<action path="/Lookup" type="journees.LookupAction" name="lookupForm" scope="session" validate="true"
input="/index.jsp">
<forward name="succes" path="/lieu.jsp"/>
<forward name="echec" path="/erreur.jsp"/>
</action>
</action-mappings>
...
INSA - ASI TechnoWeb : Struts 33/36
MessageResources.properties
# -- standard errors --
errors.header=<font color="#FF0000"><ul>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</ul></font>
# -- validator --
errors.theme=Veuillez entrer un theme a rechercher
INSA - ASI TechnoWeb : Struts 34/36
Conclusion (1/3)
Struts ? Et le reste ?
Struts n’est pas le seul outil de haut niveau disponible pour les
applications web J2EE :
Java Server Faces (JSF),
Tapestry,
Hibernate,
Spring (Spring MVC),
...
Conclusion (2/3)
Faut-il utiliser Struts ?
Conclusion (3/3)
Sources
http://struts.apache.org/
http://www-lih.univ-lehavre.fr/~coletta/dess0607/struts/une_appli.html
http://www-lih.univ-lehavre.fr/~coletta/dess0607/struts/erreurs.html
http://www.labo-sun.com/resource-fr-essentiels-859-5-java-j2ee-struts-un-framework-mvc-pour-vos-
applications-j2ee.htm\#h1n3
http://www.jmdoudoux.fr/java/dej/chap052.htm