is why I will explain in more detail how it works and set up this framework.
For this I will rely on Struts 2 project posted on the Google Group (file: MVCStruts2.rar).
first thing to do is edit the deployment descriptor (web.xml file) to add the Struts2 filter that allows to run on our project. Shall We
web.xml look like:
\u0026lt;? Xml version = "1.0" encoding = "UTF-8"?>
\u0026lt;web-app xmlns: xsi = "http://www.w3.org / 2001/XMLSchema-instance "xmlns =" \u200b\u200bhttp://java.sun.com/xml/ns/javaee "xmlns: web =" http://java.sun.com/xml/ns/javaee/web-app_2_5 . xsd "xsi: schemaLocation =" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MVCStruts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
\u0026lt;filter-mapping>
\u0026lt;filter-name> struts \u0026lt;/ filter-name>
\u0026lt;url-pattern> / * \u0026lt;/ url-pattern>
\u0026lt;/ filter-mapping>
\u0026lt;/ web-app>
In this way the web server (Tomcat in this case) know that any request made to pass through the filter FilterDispatcher, which is the controller nuestas of Web application.
Struts 2 lets you create a xml configuration file, which you can specify what to do our Controller according to the request received by the customer. This file is called struts.xml and package must be created on the default Web Project Resources: src.
The project file can be written as follows:
\u0026lt;? Xml version = "1.0" encoding = "UTF-8"?>
\u0026lt;! DOCTYPE struts PUBLIC "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN "" http://struts.apache.org/dtds/struts-2.0.dtd ">
\u0026lt;struts>
\u0026lt;include file="login-config.xml" />
\u0026lt;/ struts>
As can be seen almost there is nothing on file content, this is because you are using a xml tag called include , which allows you to import content from other Struts 2 xml configuration files. In this way we avoid that as our application grows, it does at the same rate our archive struts.xml. Now let's see the contents of the login-config.xml (which contains the login action in our application).
\u0026lt;? Xml version = "1.0" encoding = "UTF-8"?>
\u0026lt;! DOCTYPE struts PUBLIC "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN" "http:// struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
<package name="accesopackage" extends="struts-default">
<action name="login" method="validarUsuario" class="cl.struts2.login.action.LoginAction">
<result name="success">/WEB-INF/home/home.jsp</result>
<result name="error">/WEB-INF/index.jsp</result>
</action>
<action name="logout" method="cerrarSessionUsuario" class="cl.struts2.login.action.LoginAction">
<result name="success">/WEB-INF/index.jsp</result>
\u0026lt;/ action>
\u0026lt;/ package>
\u0026lt;/ struts>
This file is only telling us to run the class' cl.struts2.login.action.LoginAction 'and method' validarUsuario () 'when you get the' login 'action, and the same class, but the method' cerrarSessionUsuario () 'when you get action' logout '. In stock
login, if the implementation of the method is correct, the action returns SUCCESS, which will lead to the '/ WEB-INF/home/home.jsp', or to otherwise take us ERROR to the home page '/ WEB-INF/index.jsp'.
Now we can create our index.jsp which will make user login and call to action 'login'.
\u0026lt;% @ page language = "java" contentType = "text / html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
\u0026lt;! DOCTYPE html PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN "" http://www.w3.org/TR/html4/loose.dtd ">
\u0026lt;html>
\u0026lt;head>
\u0026lt;meta http-equiv = "Content-Type" content = "text / html; charset = ISO-8859-1">
\u0026lt;title> Log On \u0026lt;/ title>
<link rel="stylesheet" href="estilos/estilos.css" type="text/css" />
<script type="text/javascript" language="javascript">
function validar(){
var user = document.form.username.value;
var pass = document.form.password.value;
if( user!=null && pass!=null ){
document.form.submit();
}
else{
alert("Ingrese usuario y contraseƱa");
}
}
</script>
</head>
<body>
<form method="post" name="form" action="login"> \u0026lt;table
class="tabla">
\u0026lt;tr>
\u0026lt;td align="center" colspan="2" class="franja-titulo">
System Login \u0026lt;/ td>
\u0026lt;/ tr> \u0026lt;td class="texto">
\u0026lt;tr>
User:
\u0026lt;/ td>
\u0026lt;td>
\u0026lt;input type = "text" name = "username" id = "username" class = "field" text ">
\u0026lt;/ td>
\u0026lt;/ tr> \u0026lt;td class="texto">
\u0026lt;tr>
Password:
\u0026lt;/ td> \u0026lt;input type="password"
\u0026lt;td> name="password" id="password" class="campo-texto">
\u0026lt;/ td>
\u0026lt;/ tr> \u0026lt;td align="center"
\u0026lt;tr> colspan="2" class="texto-tabla">
\u0026lt;%
if (request.getAttribute ("error")! = null) {
%>
Failed to validate the data
\u0026lt;%}
%>
\u0026lt;/ td>
\u0026lt;/ tr>
\u0026lt;tr> \u0026lt;td Align="center" colspan="2">
\u0026lt;input type="button" value="Aceptar" onclick="validar();">
\u0026lt;/ td>
\u0026lt;/ tr>
\u0026lt;/ table>
\u0026lt;/ form>
\u0026lt;/ body>
\u0026lt;/ html>
As you can see in the image, the web form index.jsp redirect to action login '.
So let's see the code for our class 'cl.struts2.login.action.LoginAction'.
loginAction public class extends implements ActionSupport ServletRequestAware, ServletResponseAware {
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger private (LoginAction.class)
private HttpServletRequest request;
private HttpServletResponse response;
validarUsuario
public String () {
if (SessionUtil. existeSession (request, SessionUtil.SESSION_USUARIO)) {
logger.debug ("[validarUsuario] user already validated");
return SUCCESS;}
String username = request.getParameter ("username") String password = request
. getParameter ("password");
logger.debug ("[validarUsuario] received parameters: {user =" + username + " password = "+ password +"}");
User user = null;
if (username! = null & & password! = null) {
logger.debug (" [validarUsuario] are valid user ");
User usu = new User ();
usu.setUsername (username);
usu.setPassword (password);
ServicioUsuarios
ServicioUsuarios service = new ();
user = servicio.obtenerUsuario (usu);
} if (user ! = null) {
logger.debug ("[validarUsuario] proper validation");
SessionUtil.crearSession (request, SessionUtil.SESSION_USUARIO, user, 15);
return SUCCESS;}
SessionUtil.cerrarSession (request);
request.setAttribute ("error", "1");
logger.debug ("[validarUsuario] error when validating user");
return ERROR;}
cerrarSessionUsuario public String () {
logger.debug ("[cerrarSessionUsuario] Close user session");
SessionUtil.cerrarSession (request);
return SUCCESS;}
getServletRequest public HttpServletRequest () {return
this.request;
}
getServletResponse public HttpServletResponse () {return
this.response;
} public void
setServletResponse (HttpServletResponse response) {
this.response = response;
} public void
setServletRequest (HttpServletRequest request) {
this.request = request;
}}
The code can see the class and method loginAction validarUsuario () , which will run under the 'login' action. I will not explain in detail the contents of this method (as with the previously published material ought not have trouble understanding this;)), but it is understood that somehow validates the user's username and password. If the validation is successful the action returns SUCCESS, otherwise ERROR (if you doubt this becomes to see the photo of the login-config.xml file).
will detail later how the interceptor and how to run the AJAX calls. Atento
their doubts. Greetings .-