Some times
you may want to trim spaces for request parameters. By using custom interceptor
you can trim request parameters.
Following is
the custom interceptor, which trim spaces.
package learn.struts; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class TrimInterceptor extends MethodFilterInterceptor { private static final long serialVersionUID = 1L; /* Update this list to exclude any request parameters from trimming */ private List<String> excludedParams = new ArrayList<String>(); public boolean isParamIncluded(String param) { for (String exclude : excludedParams) { if (param.startsWith(exclude)) { return false; } } return true; } @Override protected String doIntercept(ActionInvocation invocation) throws Exception { Map<String, Object> parameters = invocation.getInvocationContext() .getParameters(); for (String param : parameters.keySet()) { if (isParamIncluded(param)) { String[] vals = (String[]) parameters.get(param); for (int i = 0; i < vals.length; i++) { vals[i] = vals[i].trim(); } } } return invocation.invoke(); } }
Update struts.xml
for custom interceptor.
<interceptors> <interceptor name="spaceTrimmer" class="learn.struts.TrimInterceptor" /> <interceptor-stack name="newStack"> <interceptor-ref name="spaceTrimmer" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors>
Following
procedure explains complete working application in Eclipse.
Step 1: Create new dynamic web project struts_tutorial in
eclipse.
File ->
New -> Dynamic Web Project.
Step 2: Convert the project struts_tutorial to maven. Right
click on the project -> Configure -> Convert to Maven project.
Total
project structure looks like below.
Step 3: Open pom.xml and update maven dependencies for
struts2. I am going to use following dependency.
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.20</version> </dependency>
Step 4: In struts2 environment, we should handover the
requests to struts2. We do this by configuring web.xml. Following is complete
web.xml file.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts_tutorial</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>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
As you
observe web.xml, we are forwarding all the requests to struts2 environment by
using filter mapping. So now it is struts2 responsibility to process request.
Step 5: Define index.html.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Simple struts2 Application</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form method="post" action="hello"> <input type="text" name="firstName" /> <input type="text" name="lastName" /> <input type="submit" value="submit" /> </form> </body> </html>
Step 6: Define helloWorld.jsp like below.
<%@ 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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Hello World</h1> <% String name = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); System.out.println(name + " " + name.length()); System.out.println(lastName + " " + lastName.length()); %> </body> </html>
Step 7: Create package ‘learn.struts’ and define HelloWorldAction
class.
package learn.struts; public class HelloWorldAction { public String execute(){ return "hello"; } }
Step 8: Define TrimInterceptor.java in package ‘learn.struts’.
package learn.struts; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class TrimInterceptor extends MethodFilterInterceptor { private static final long serialVersionUID = 1L; /* Update this list to exclude any request parameters from trimming */ private List<String> excludedParams = new ArrayList<String>(); public boolean isParamIncluded(String param) { for (String exclude : excludedParams) { if (param.startsWith(exclude)) { return false; } } return true; } @Override protected String doIntercept(ActionInvocation invocation) throws Exception { Map<String, Object> parameters = invocation.getInvocationContext() .getParameters(); for (String param : parameters.keySet()) { if (isParamIncluded(param)) { String[] vals = (String[]) parameters.get(param); for (int i = 0; i < vals.length; i++) { vals[i] = vals[i].trim(); } } } return invocation.invoke(); } }
Step 9: It is time to create struts.xml. Since Struts 2
requires struts.xml to be present in classes folder. So create struts.xml file
under the WebContent/WEB-INF/classes folder. Eclipse does not create the
"classes" folder by default, so you need to do this yourself. To do
this, right click on the WEB-INF folder in the project explorer and select New
> Folder.
Create
struts.xml file inside classes.
struts.xml<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <interceptors> <interceptor name="spaceTrimmer" class="learn.struts.TrimInterceptor" /> <interceptor-stack name="newStack"> <interceptor-ref name="spaceTrimmer" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <action name="hello" class="learn.struts.HelloWorldAction"> <interceptor-ref name="newStack" /> <result name="hello">helloWorld.jsp</result> </action> </package> </struts>
Total
project structure looks like below.
Step 10: Run application on server. Give some input by
adding spaces before and after, you can see the string and its length after
trimming in console.
For above
input, I got following message in console.
abcd 4
dfghjik 7
No comments:
Post a Comment