Struts Application- Logic Tag Example
Aim:
To perform logical operation in struts application using Logic tag.
< logic:empty >
< logic:notempty >
< logic:match >
< logic:notmatch >
All above mentioned tag have some common attributes name property.
But < logic:match > and < logic:notmatch > have additional attributes that is value.
Procedure:
Step_1:
Create actionforms(I have used actionforms name as logicforms).
Step_2:
Create actions(I have used actin name as logicactions).
Step_3:
Create jsp files
A). Home.jsp
B). success.jsp
Step_4:
Made changes in struts-config.xml
Step_5:
Run Home.jsp
Explain:
Step_1:
Create actionforms
Already I have explained how to create actionforms. You follow that rules.
package actionforms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class logicforms extends org.apache.struts.action.ActionForm
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Step_2:
Create action
I have already explained how to create action.
package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class logicactions extends org.apache.struts.action.Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
return mapping.findForward("success");
}
}
Step_3:
Create jsp files.
A) Home.jsp
< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
< html:form action="/logic" >
< html:text property="name"/ >
< html:submit value="OK"/ >
< /html:form >
< /body >
< /html >
B). success.jsp
< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
< logic:empty property="name" name="logicforms" >
I am in logic:empty Tag.
< /logic:empty >
< br >
< logic:notEmpty property="name"name="logicforms" >
I am in logic:notEmpty Tag.
< /logic:notEmpty >
< br >
< logic:match property="name" value="suresh" name="logicforms" >
Hello Suresh
< /logic:match >
< br >
< logic:notMatch name="logicforms" property="name" value="sures" >
Hello! da ! You aer not match
< /logic:notMatch >
< /body >
< /html >
Step_4:
Made changes in struts-config.xml
< action-mappings >
< action input="/" name="logicforms" path="/logic" scope="session" type="actions.logicactions" >
< forward name="success" path="/jsp/success.jsp"/ >
< /action >
< /action-mappings >
Step_5:
Run Home.jsp
Conclusion:
From this tag we have learnt the logic:empty,logic:notempty , logic:match, logic:notmatch.
You have any doubt please post comment. I clarify your doubt.