struts2

1. Prepare struts2 files and setup Eclipse

1.1 Install web server(GlassFish) for Eclipse

Install “GlassFish Tools for Kepler” in “Eclipse Marketplace”. When installation succeed, a “GlassFish” icon will appear in “Menu” -> “Help”.

create “Dynamic Web Project” in Eclipse, “Runtime” -> “New Runtime” -> “GlassFish 4.0”, download GlassFish and install it.

1.2 Download struts2 jar files

Download struts2 jar files from http://struts.apache.org/development/2.x/

Some guides on struts2 http://www.tutorialspoint.com/struts_2/struts_configuration.htm

2. First “Dynamic Web Project” on Glassfish

(1). create project

Project Name: web1:  
default output folder: build\classes  
Context root: web1 (config in WebContent/WEB-INF/glassfish-web.xml  
Content Directory: WebContent  
Generate web.xml depoyment description.  

(2). add simple index.jsp

(3). run on GlassFish

3. First struts2 web project

1. create dynamic web project as in previous chapter

We need struts2 core runtime jars, the core jars can be extracted from struts2-blank.war(in struts-2.x.x-apps.zip).
Add core struts2 jar into build path
Copy core struts2 jar at WEB-INF/lib.

2. add filter into web.xml

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

First we create an filter named “struts2”, it’s backed by class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.
Now we map all /* url to the filter named “struts2”, then all requests to url /* will be feed to class o.a.s.d.n.f.StrutsPrepareAndExecuteFilter.

3. index.jsp

add this into the beginning of the xml, this will support struts2 taglib:

<%@taglib prefix="s" uri="/struts-tags"%>

Then ‘s:form’ can be reconiged in jsp.

add this into <body>, create a simple login form

<H2>System Entry</H2>
<s:form action="loginAction">
    <s:textfield name="theName" label="User"></s:textfield>
    <s:password name="thePwd" label="Password"></s:password>
    <s:submit value="OK"></s:submit>
    <s:reset value="clear"></s:reset>
</s:form>

It creates a simple login webpage. When user click “submit”, the request will be sent to loginAction, with the text fields theName/thePwd passed to the java String variables in action class.
jsp files configure the struts2 actions which can handle web requests.

4. create loginAction

create loginAction(com.LoginAction) which extends ActionSupport, this class will handle requests from index.jsp.

members:

private String theName;
private String thePwd;

theName and thePwd are configured in index.jsp, they will be passed to com.LoginAction when user click “OK” in login screen.

functions:

@Override
public String execute() throws Exception {
    String ret = null;
    if (theName.equals("admin") && thePwd.equals("1")) {
        ActionContext.getContext().getSession().put("login", "true");
        ret = "success";
    } else {
        ret = "fail";
    }

    return ret;
}

execute() will set an ActionContext variable “login” if login success, then return a string to web. The returned string will be used to determine next jsp page.(success->/success.jsp, fail->/fail.jsp, as configured in struts.xml)

5. create struts2 config file: struts.xml

struts.xml configures the jsp files which will be returned to user after action is executed.

Note:
struts.xml should be place at the src/ directory.
PUBLIC ID of DOCTYPE must exactly match the standard struts-2.3.dtd.

Create “struts.xml” from struts-2.3.dtd:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "struts-2.3.dtd" >
<struts>
    <package extends="struts-default" name="struts2">
        <action class="com.LoginAction" name="loginAction">
            <result name="success">/success.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>
    </package>
</struts>

It creates a package which extends “struts-default”, the package has an actions named “loginAction”(backed by com.LoginAction).
If loginAction returns java String “success”, then the web page will switch to “/success.jsp”, if returns java string “fail”, then “/fail.jsp”.

6. create success.jsp and fail.jsp

The 2 files can be very simple, they only provide some infomation about the status of login(success or fail).