Quick Contact

    Hibernate

    Struts 2 – Actions

    Actions are the core of the Struts2 framework, as they are for any MVC (Model View Controller) framework. Each URL is mapped to a specific action, which provides the processing logic which is necessary to service the request from the user.

    But the action also serves in two other important capacities. Firstly, the action plays an important role in the transfer of data from the request through to the view, whether its a JSP or other type of result. Secondly, the action must assist the framework in determining which result should render the view that will be returned in the response to the request.

    Create Action

    The only requirement for actions in Struts2 is that there must be one noargument method that returns either a String or Result object and must be a POJO. If the no-argument method is not specified, the default behavior is to use the execute() method.

    Optionally you can extend the ActionSupport class which implements six interfaces including Action interface. The Action interface is as follows −

    publicinterfaceAction{
    publicstaticfinalString SUCCESS ="success";
    publicstaticfinalString NONE ="none";
    publicstaticfinalString ERROR ="error";
    publicstaticfinalString INPUT ="input";
    publicstaticfinalString LOGIN ="login";
    publicString execute()throwsException;
    }
    
    Let us take a look at the action method in the Hello World example −
    package com.ducatindia.struts2;
    
    publicclassHelloWorldAction{
    privateString name;
    
    publicString execute()throwsException{
    return"success";
    }
    
    publicStringgetName(){
    return name;
    }
    
    publicvoidsetName(String name){
    this.name = name;
    }
    }
    

    To illustrate the point that the action method controls the view, let us make the following change to the execute method and extend the class ActionSupport as follows −

    package com.ducatindia.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    publicclassHelloWorldActionextendsActionSupport{
    privateString name;
    
    publicString execute()throwsException{
    if("SECRET".equals(name)){
    return SUCCESS;
    }else{
    return ERROR;
    }
    }
    
    publicStringgetName(){
    return name;
    }
    
    publicvoidsetName(String name){
    this.name = name;
    }
    }
    
    

    In this example, we have some logic in the execute method to look at the name attribute. If the attribute equals to the string “SECRET”, we return SUCCESS as the result otherwise we return ERROR as the result. Because we have extended ActionSupport, so we can use String constants SUCCESS and ERROR. Now, let us modify our struts.xml file as follows −

    < ?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>
    < constantname="struts.devMode"value="true"/>
    < packagename="helloworld"extends="struts-default">
    < actionname="hello"
    class="com.ducatindia.struts2.HelloWorldAction"
    method="execute">
    < resultname="success">/HelloWorld.jsp< /result>
    < resultname="error">/AccessDenied.jsp< /result>
    < /action>
    < /package>
    < /struts>
    

    Create a View

    Let us create the below jsp file HelloWorld.jsp in the WebContent folder in your eclipse project. To do this, right click on the WebContent folder in the project explorer and select New >JSP File. This file will be called in case return result is SUCCESS which is a String constant “success” as defined in Action interface −

    <%@ page contentType="text/html; charset = UTF-8" %>
    <%@taglib prefix ="s"uri="/struts-tags" %>
    
    < html>
    < head>
    < title>Hello World< /title>
    < /head>
    
    < body>
          Hello World, < s:propertyvalue="name"/>
    < /body>
    < /html>
    

    Following is the file which will be invoked by the framework in case action result is ERROR which is equal to String constant “error”. Following is the content of AccessDenied.jsp

    <%@ page contentType="text/html; charset = UTF-8" %>
    <%@taglib prefix ="s"uri="/struts-tags" %>
    
    < html>
    < head>
    < title>Access Denied< /title>
    < /head>
    
    < body>
          You are not authorized to view this page.
    < /body>
    < /html>
    

    We also need to create index.jsp in the WebContent folder. This file will serve as the initial action URL where the user can click to tell the Struts 2 framework to call the executemethod of the HelloWorldAction class and render the HelloWorld.jsp view.

    <%@ page language ="java"contentType="text/html; charset = ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@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>
    < title>Hello World< /title>
    < /head>
    
    < body>
    < h1>Hello World From Struts2< /h1>
    < formaction="hello">
    < labelfor="name">Please enter your name< /label>< br/>
    < inputtype="text"name="name"/>
    < inputtype="submit"value="Say Hello"/>
    < /form>
    < /body>
    < /html>
    

    That’s it, there is no change required for web.xml file, so let us use the same web.xml which we had created in Examples chapter. Now, we are ready to run our Hello World application using Struts 2 framework.

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.