15 April 2017

Struts 2 Framework: Interceptor Console Basic


Add user Library
struts.xml
Java Web 2017
<?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.custom.i18n.resources" value="LoginAction" />

    <package name="default" extends="struts-default" namespace="/">
        <interceptors>
            <interceptor name="firstInterceptor"
                         class="com.giaima.FirstInterceptor" />
            <interceptor name="secondInterceptor"
                         class="com.giaima.SecondInterceptor" />

        </interceptors>

        <action name="Dummy" class="com.giaima.DummyAction">
            <interceptor-ref name="firstInterceptor" />
            <interceptor-ref name="secondInterceptor" />
            <result name="success">Welcome.jsp</result>
            <result name="input">login.jsp</result>
        </action>
    </package>
</struts>
web.xml
Java Web 2017
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>InterceptorsWorkFlow</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>Test.jsp</welcome-file>
    </welcome-file-list>
</web-app>
Test.jsp
Java Web 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Dummy Action</title>
    </head>
    <body bgColor="lightBlue">
    <s:form action="Dummy">
        <s:submit value="For calling Dummy Action" align="center" />
    </s:form>
</body>
</html>
Welcome.jsp
Java Web 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
        <title>Welcome</title>
    </head>
    <body>
        Welcome !!!
    </body>
</html>
DummyAction.java
Java Web 2017
package com.giaima;

import com.opensymphony.xwork2.ActionSupport;

public class DummyAction extends ActionSupport {

    public String execute() {
        return SUCCESS;
    }

}
FirstInterceptor.java
Java Web 2017
package com.giaima;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class FirstInterceptor implements Interceptor {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
      
        String startInterceptor = "   Start Interceptor 1";
        System.out.println(startInterceptor);
        String result = actionInvocation.invoke();

        String endInterceptor = "   End Interceptor 1";
        System.out.println(endInterceptor);
        return result;
    }

}
SecondInterceptor.java
Java Web 2017
package com.giaima;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SecondInterceptor implements Interceptor {

    @Override
    public void destroy() {

    }

    @Override
    public void init() {

    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {

        String startInterceptor = "   Start Interceptor 2";
        System.out.println(startInterceptor);
        String result = actionInvocation.invoke();

        String endInterceptor = "   End Interceptor 2";
        System.out.println(endInterceptor);
        return result;
    }

}
Run test

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang