Showing posts with label struts2. Show all posts
Showing posts with label struts2. Show all posts

Difference between invoke() and invokeActionOnly() methods

In any Interceptor class invoke() will includes interceptors, action and result execution, while invokeActionOnly() skip the interceptor and go straight to the action.

For ex:-

System.out.println("Before Interceptor invoke");

String result = invocation.invoke();

System.out.println("After Interceptor invoke");

In this After interceptor invokation It will execute action then again it comes to interceptor and displays message "After Interceptor invoke"

System.out.println("Before Interceptor invoke");

String result = invocation.invokeActionOnly();

System.out.println("After Interceptor invoke");

In this After interceptor invocation It will execute action then It will skip the interceptor.If it is in first interceptor of interceptor stack.Then after first interceptor in stack it will skip execution and Other interceptors in stack will never call.

How to send parameters to action in Freemarker and webwork

How to send parameters to action in freemarker

success.ftl

<b>Success</b>
<br/>

<@ww.url id="idsuc" action="success1"/>
<@ww.url id="idsuccess1" action="%{idsuc}">
<@ww.param name="name" value="%{name}"/>
</@ww.url>

<@ww.a href="%{idsuccess1}"> Click To Success1</@ww.a>

success1.ftl

<b>Success1</b><br/>

<b>The value You Entered is : <@ww.text name="%{name}"/></b>

<br/>
<b>
Test Param is:
<#if parameters.name?exists>
${name}
</#if>
</b><br/>

Custom Validation Example in Struts2 and webwork

CustomLoginAction.java

package com.login;

import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork.ActionSupport;

public class CustomLoginAction extends ActionSupport {

private User user;

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public String testexecute() {
if (StringUtils.equals(user.getUserName(),"test") &&

StringUtils.equals(user.getPwd(),"test")) {
return SUCCESS;
}
else
return "failure";
}}

LoginAction.java

package com.login;

import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Validateable;

public class LoginAction extends ActionSupport implements Validateable{

private User user;

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public String testexecute() {
if (StringUtils.equals(user.getUserName(),"akbar") &&

StringUtils.equals(user.getPwd(),"santhi")) {
return SUCCESS;
}
else
return "failure";
}

public void validate() {
User user = getUser();
if (StringUtils.isBlank(user.getUserName())) {
addActionError(getText("user.name.empty"));
}
if (StringUtils.isBlank(user.getPwd())) {
addActionError(getText("user.pwd.empty"));
}
}

}

User.java

package com.login;

public class User {

String userName="";
String pwd="";
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}

Validation. xml file name must be with the name of action.(i.e if action name is test then validation.xml name is test-validation.xml)

CustomLoginAction-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.userName" >
<field-validator type="requiredstring">
<message key="user.name.empty">resource not found</message>
</field-validator>
</field>
<field name="user.pwd">
<field-validator type="requiredstring">
<message key="user.pwd.empty"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<message key="user.pwd.length"/>
</field-validator>
</field>
</validators>

Validation properties file name must be with the name of action.(i.e if action name is test then properties file name is test.properties)

CustomLoginAction.properties

user.name.empty=Enter your Name
user.pwd.empty= Enter your Password
user.pwd.length=Password must be atleast ${minLength} characters long

LoginAction.properties

user.name.empty=Please enter your Name
user.pwd.empty=Please enter your Password

validators.xml

Here you can specify your own validator.

<validators>
<validator name="required"

class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring"

class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
<validator name="int"

class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
<validator name="date"

class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>
<validator name="expression"

class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression"

class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
<validator name="email"

class="com.opensymphony.xwork.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>
<validator name="visitor"

class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>
<validator name="conversion"

class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength"

class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>

<validator name="customvalidator"

class="com.login.CustomLoginAction"/>

</validators>

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"

"http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>

<include file="webwork-default.xml"/>

<package name="default" extends="webwork-default">
<default-interceptor-ref name="validationWorkflowStack"/>
<action name="login" class="LoginAction1" method="testexecute">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="CustomLoginAction1" class="CustomLoginAction" method="testexecute">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/index1.jsp</result>
</action>
</package>
</xwork>

You can use struts tags here in all jsps like this .

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

<s:form action="CustomLoginAction1">


index.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<h1>
Login Form
</h1>
<hr/>
<%@ include file="/header.jsp" %>
<ww:form action="login" method="POST">
<ww:textfield name="user.userName" label="User"/>
<ww:password name="user.pwd" label="Password"/>
<ww:submit value="Submit"/>
</ww:form>

header.jsp

<!--
http://wiki.opensymphony.com/display/WW/WebWork+2+UI+Tag+Guide?decorator=printable

-->

<ww:if test="hasErrors()">
<p style="color: red;">
<b>Errors:</b>
<ul>

<ww:if test="hasActionErrors()">
<ww:iterator value="actionErrors">
<li style="color: red;"><ww:property/></li>
</ww:iterator>
</ww:if>

<ww:if test="hasFieldErrors()">
<ww:iterator value="fieldErrors">
<ww:iterator value="value">
<li style="color: red;"><ww:property/></li>
</ww:iterator>
</ww:iterator>
</ww:if>
</ul>
</p>
</ww:if>

index1.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<h1>
Login Form
</h1>
<hr/>
<%@ include file="/header.jsp" %>
<ww:form action="CustomLoginAction1" method="POST">
<table>
<tr>
<td>Name</td>
<td><ww:textfield theme="simple" name="user.userName"/></td>
</tr>
<tr>
<td>Password</td>
<td><ww:password theme="simple" name="user.pwd"/></td>
</tr>

<tr>
<td></td>
<td><ww:submit theme="simple" value="Submit"/></td>
</tr>
</table>
</ww:form>

success.jsp

<%@ taglib uri="webwork" prefix="ww" %>
<p><b>
Hi <ww:property value="user.userName"/> <br/>
You Successfully Logged
</b></p>

failure.jsp

<h2>
Login Failure Please verify your UserName and Password
</h2>

How to Use Macros in Freemarker

How to Use Macros in Freemarker

macros.ftl


<#macro greet>
<font color="red">Hello World</font>
</#macro>

<#macro greet1 person>
<font color="blue">Hello ${person}!</font>
</#macro>

<#macro greet2 person color="green">
<font color="${color}">Hello ${person}!</font>
</#macro>

<#macro border>
<table border=4 cellspacing=0 cellpadding=4><tr><td>
<#nested>
</tr></td></table>
</#macro>

<#macro thrice>
<#nested><br/>
<#nested><br/>
<#nested><br/>
</#macro>

<#macro twise>
<ul><li><#nested 1></li>
<li><#nested 2></li>
</ul>
</#macro>

form.ftl

<html>
<body>
<#include "macros.ftl">

<@greet/><br/>

<@greet1 person="santhi"/><br/>

<@greet2 person="prasanthi" color="orange"/>

<@border>Test</@border>

<@thrice>
Thrice.
</@thrice>

<@twise ; x>
${x}. Twise.
</@twise>

</body>
</html>

Custom Interceptors example in webwork and struts2

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">

<interceptors>
<interceptor name="greeting" class="com.hello.GreetingInterceptor">
</interceptor>

<interceptor name="nextinterceptor" class="com.hello.NextInterceptor" />
<interceptor name="thirdinterceptor" class="com.hello.ThirdInterceptor" />
<interceptor name="stack1interceptor" class="com.hello.Stack1Interceptor" />
<interceptor name="stack2interceptor" class="com.hello.Stack2Interceptor" />
<interceptor name="beforeafterinterceptor" class="com.hello.BeforeAfterInterceptor" />

<interceptor-stack name="TestStack">
<interceptor-ref name="stack1interceptor"/>
<interceptor-ref name="stack2interceptor" />
<interceptor-ref name="stack2interceptor"/>
</interceptor-stack>

</interceptors>

<action name="hello" class="com.hello.HelloAction" method="execute">
<interceptor-ref name="greeting"/>
<result name="success">/success.jsp</result>
</action>

<action name="stack" class="com.hello.StackAction" method="execute">
<result name="success">/stacksuccess.jsp</result>
<interceptor-ref name="TestStack" />
</action>

<action name="beforeafter" class="com.hello.BeforeAfterAction" method="execute">
<result name="success">/beforeafter.jsp</result>
<interceptor-ref name="beforeafterinterceptor" />
</action>

</package>
</xwork>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/lib/webwork-2.2.2.jar</taglib-location>
</taglib>
</web-app>

GreetingInterceptor.java

package com.hello;

import java.util.Map;

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

public class GreetingInterceptor implements Interceptor {

public void init() {
}

public void destroy() {
}

public String intercept(ActionInvocation invocation) throws Exception {
String greeting = "Hello World Interceptor4444";

invocation.getInvocationContext().getSession().put("hello", greeting);

String result = invocation.invoke();

return result;
}

}

HelloAction.java

package com.hello;

import com.opensymphony.xwork.ActionSupport;

public class HelloAction extends ActionSupport {

public String execute() {

System.out.println("Action Calling");

return "success";

}
}

StackAction.java

package com.hello;

import com.opensymphony.xwork.ActionSupport;

public class StackAction extends ActionSupport {

public String execute() {

System.out.println("Stack Action Calling");

return SUCCESS;

}
}

Stack1Interceptor.java

package com.hello;

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

public class Stack1Interceptor implements Interceptor{
String test="hello";

public String getTest() {
return test;
}

public void setTest(String test) {
this.test = test;
}

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String stack1="Stack1 Interceptor" ;

invocation.getInvocationContext().getSession().put("stack1", stack1);

System.out.println("Stack interceptor1 --------- "+stack1+" --- ");

String result = invocation.invoke();

System.out.println("After Invocation Stack interceptor1 ---------");

return result;
}

}

Stack2Interceptor.java

package com.hello;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class Stack2Interceptor implements Interceptor{

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String stack2="Stack2 Interceptor" ;

invocation.getInvocationContext().getSession().put("stack2", stack2);

System.out.println("Stack interceptor2 --------- "+stack2+" --- ");

String result = invocation.invoke();

System.out.println("After Invocation Stack interceptor2 ---------");

return result;
}

}
BeforeAfterInterceptor.java

package com.hello;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;

public class BeforeAfterInterceptor extends AroundInterceptor
{
protected void after(ActionInvocation invoke, String arg1) throws Exception {
System.out.println("After Invocation BeforeAfterInterceptor");
}

protected void before(ActionInvocation invoke) throws Exception {
System.out.println("Before Invocation BeforeAfterInterceptor");

}

}

BeforeAfterAction.java

package com.hello;

import com.opensymphony.xwork.Action;

public class BeforeAfterAction implements Action{

public String execute() throws Exception {

System.out.println("come to Before After Action");
return SUCCESS;
}

}

NextInterceptor.java

package com.hello;

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

public class NextInterceptor implements Interceptor{

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

String greeting1="Next Interceptor" ;

String first=(String) invocation.getInvocationContext().getSession().get("greeting");
System.out.println("Getting session valueeeee --- "+first);

invocation.getInvocationContext().getSession().put("next", greeting1);

System.out.println("Test interceptor2 --------- "+greeting1+" --- ");

String result = invocation.invoke();
//System.out.println("the resullt isssss 222222222"+result);
return result;
}

}

beforeafter.jsp

<hr/>
<h2>Success</h2>

index.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<hr/>
<center>
<a href="hello.action">Click Here</a><br/><br/>
<a href="stack.action">Click Here To Test Stack</a>
<br/><br/>
<a href="beforeafter.action">Click Here To Test Before After Action</a>
</center>
<hr/>

stacksuccess.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<!--
<b>Stack Hello</b> <br/>
<b>
<ww:text name="${#session.stack1}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.stack2}"></ww:text>
</b><br/>-->

success.jsp

<%@ taglib uri="webwork" prefix="ww" %>

<b>Hello</b> <br/>

<b>
<ww:text name="${#session.greeting}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.next}"></ww:text>
</b><br/>

<b>
<ww:text name="${#session.third}"></ww:text>
</b>

<b>
<ww:text name="${#session.greeting2}"></ww:text>
</b>