El ejercicio que vamos a realizar es un ejemplo de hola mundo con struts realizado con maven para la librerías y eclipse como IDE.
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.2</version> </dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutosoftware</groupId> <artifactId>HolaStruts</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HolaStruts Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>HolaStruts</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
package com.tutosoftware.holastruts.action; import com.opensymphony.xwork2.ActionSupport; public class HolaAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String nombre; private String mensaje; @Override public String execute() throws Exception { mensaje = "Hola bienvenido a struts 2:"; return ActionSupport.SUCCESS; } @Override public void validate() { if (null == nombre || nombre.length() == 0) addActionError(getText("error.nombre.requerido")); } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } public String getMensaje() { return mensaje; } public void setMensaje(String mensaje) { this.mensaje = mensaje; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts2 hola mundo</title> <s:head/> </head> <body> <h1 style="color : red"> <s:text name="label.saludo"></s:text> </h1> <s:if test="hasActionErrors()"> <div id="campoError"> <s:actionerror/> </div> </s:if> <s:form action="holaAction" namespace="/" method="post" name="holaForm"> <s:textfield name="nombre" size="30" maxlength="50" key="label.nombre"></s:textfield> <s:submit key="enviar" align="rigth"></s:submit> </s:form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts2 maven hola mundo</title> </head> <body> <h2 style="color: blue"> <s:property value="mensaje" /> </h2> <h1 style="color: blue"> Saludos <s:property value="nombre" /> </h1> <a href="index.jsp">Inicio</a> </body> </html>
label.saludo = Struts 2 Maven Hola Mundo! label.nombre = Nombre error.nombre.requerido = Nombre es requerido!
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <constant name="struts.devMode" value="true"></constant> <constant name="struts.custom.i18n.resources" value="ApplicationResources"></constant> <package name="default" extends="struts-default"> <action name=""> <result>index.jsp</result> </action> <action name="holaAction" class="com.tutosoftware.holastruts.action.HolaAction"> <result name="error">index.jsp</result> <result name="input">index.jsp</result> <result name="success">/jsp/exito.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="jst.web" version="3.1"/> <installed facet="wst.jsdt.web" version="1.0"/> <installed facet="java" version="1.8"/> </faceted-project>