Struts 2.x : [A Framework from ASF] :: Struts 2.x with Interceptor
Struts 2.x with Interceptor |
Struts 2.x with Interceptor Example that accept username and convert into uppercase while displaying.
Example for Struts 2.x with Interceptor |
Project Structure
Login.java
package com.javaskool.struts2;
public class Login {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute(){
return "success";
}
}
MyInterceptor.java
package com.javaskool.struts2;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
public class MyInterceptor implements Interceptor{
public void init() {}
public String intercept(ActionInvocation ai) throws Exception {
ValueStack stack=ai.getStack();
String s=stack.findString("name");
stack.set("name",s.toUpperCase());
return ai.invoke();
}
public void destroy() {}
}
index.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="login">
<s:textfield name="name" label="Username : "></s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
Welcome, <s:property value="name"/>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myPack1" extends="struts-default">
<interceptors>
<interceptor name="myUpperInterceptor"
class="com.javaskool.struts2.MyInterceptor"></interceptor>
</interceptors>
<action name="login" class="com.javaskool.struts2.Login">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myUpperInterceptor"></interceptor-ref>
<result>welcome.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?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>Struts2Interceptor4UppercaseConversion</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [3.5 MB]
The discussion of frame work its really helpful.These snapshot also really very clearly explains about the concept.