Results 1 to 5 of 5

Thread: Beginning with the Struts 2

  1. #1
    Join Date
    May 2009
    Posts
    539

    Beginning with the Struts 2

    We will make 3 JSP pages:
    • index.jsp: contains just a link and a button.
    • login.jsp: the form of user input.
    • principal.jsp: the test result page.

    We will also need a class:
    • Login.java: this will be our Action. Struts 2 does not require that inherits functionality from another class or implement any interface, but not to give work to the reflection, I do to inherit functionality ActionSupport.

    JAR packages installed in the project lib
    • Struts2-core-2.0.8.jar
    • xwork-2.0.3.jar
    • freemaker-2.3.8.jar
    • OGNL-2.6.11.jar
    • commons-logging-1.1.jar

  2. #2
    Join Date
    May 2009
    Posts
    539

    Re: Beginning with the Struts 2

    login.jsp

    I will use Struts tags here, for which I need the tag directive:
    Code:
    <% @ Taglib prefix = "s" uri = "/ struts-tags"%> 
    And among the labels: 
    action="Login"> <s:form 
    key="usuario" <s:textfield registered label="Usuario :"> 
    key="password" <s:password label="Contraseña"> 
    <s:submit/> 
    </ S: form>
    The only important consideration here is action = "Login". It happens that for Struts to verify that it is invoking an Action, should be invoked (via GET or POST) to Login.action, however, and because we are using Struts tags, this login will appear correctly when The JSP is processed. But this is not entirely true, since if the JSP page is not displayed to the user as a result of the invocation of some action, will continue rising in the resulting HTML an action = "Login" and this will result in a 404. Wondering why? Me too, but for now I have no answer ... Index.jsp and principal.jsp pages are nothing special, so the saltearé almost with contempt.

  3. #3
    Join Date
    May 2009
    Posts
    539

    Re: Beginning with the Struts 2

    Login.java class

    This is the action itself, here's the code:
    Code:
    package prueba;
    
    import com.opensymphony.xwork2.ActionSupport;;
    
    public class Login extends ActionSupport{
    
      private String usuario;
      private String password;
    
      @Override
      public String execute() throws Exception {
      
        System.out.println("procesando el execute de Login");
        System.out.println("Usuairo: " + usuario);
        System.out.println("Password: " + password);
        
        return SUCCESS;
      }
    
      public String getUsuario() {
        return usuario;
      }
    
      public void setUsuario(String usuario) {
        this.usuario = usuario;
      }
    
      public String getPassword() {
        return password;
      }
    
      public void setPassword(String password) {
        this.password = password;
      }
    }

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: Beginning with the Struts 2

    It is important that we do not need ActionForms or any other type of class that represents the form to be treated. Here Struts 2 uses the Dependency Injection pattern to fill the attributes you need, for that reason, if we take into account the creation of a get / set for each component of presentation we want to treat. With regard to the method execute (), you see not receive parameters and returns a string representing the result of the action (error, success or any other string).

  5. #5
    Join Date
    May 2009
    Posts
    539

    Re: Beginning with the Struts 2

    All very nice, but how configure it?. File struts.xml

    Code:
    <!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 name="struts.devMode" value="false"/>
    <package name="example" namespace="/" extends="struts-default">
    <action name="Welcome">
    <result>
    /login.jsp
    </result>
    </action>
    <action name="Login" class="prueba.Login">
    <result name="input">
    /login.jsp
    </result>
    <result name="error">
    /login.jsp
    </result>
    <result>
    /principal.jsp
    </result>
    </action>
    </package>
    </struts>
    What is important are the labels:
    • package: it symbolizes a work unit is exactly the same way as packages in UML and Java.
    • action: the action itself
    • result: the possible outcome. If we do not include a name, will be used by default when the action results in SUCCESS.

    OK, I was already very long, I leave the source to play a bit and next post we will see how to access the session, request, etc.

Similar Threads

  1. Struts 1 VS Struts 2
    By PsychoVillan in forum Software Development
    Replies: 6
    Last Post: 29-03-2011, 12:23 AM
  2. JSF versus Struts
    By Daksha in forum Software Development
    Replies: 3
    Last Post: 21-07-2010, 06:20 AM
  3. How to mix Eclipse, WTP, Struts, and Hibernate
    By Efigenio in forum Software Development
    Replies: 5
    Last Post: 21-02-2010, 05:34 AM
  4. How to upgrade one of struts in JDEV 10.1.3
    By GeforceUser in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 05:45 PM
  5. Struts Forward Action with example
    By Visala28 in forum Software Development
    Replies: 2
    Last Post: 10-08-2009, 05:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,859,003.08991 seconds with 17 queries