12:31 am - Monday May 1, 2017

Struts logic present logic notPresent example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure:

   

 

Jar files:

 

 

 

 

 

 

 

 

 

 

 

LogicPresentAction.java:

package com.candidjva;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LogicPresentAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession ses = request.getSession(true);

		User m = (User) form;
		String s = m.getMessage();
		request.setAttribute("user", s);

		return mapping.findForward("success");
	}

//}

User.java:

package com.candidjva;

import org.apache.struts.action.ActionForm;

public class User extends ActionForm {

	String message = "message property present";

	public String getMessage() {
		return message;
	}

	public String setMessage(String message) {
		return this.message = message;
	}

}

Web.xml:

<!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>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>

		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>welcome.jsp</welcome-file>

	</welcome-file-list>

	<taglib>
		<taglib-uri>/struts-html.tld</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/struts-bean</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/struts-logic</taglib-uri>
		<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
	</taglib>

	<security-constraint>
		<web-resource-collection>
			<web-resource-name>My secure resources</web-resource-name>
			<description>Resources to be placed under security control.</description>
			<url-pattern>/private/*</url-pattern>
			<url-pattern>/registered/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>guest</role-name>
		</auth-constraint>
	</security-constraint>

	<login-config>
		<auth-method>BASIC</auth-method>
		<realm-name>WebApp</realm-name>
	</login-config>

	<security-role>
		<description>The role allowed to access our content</description>
		<role-name>guest</role-name>
	</security-role>

</web-app>

Struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean type="com.candidjva.User" name="model" />
	</form-beans>

	<action-mappings>

		<action path="/LogicTest" type="com.candidjva.LogicPresentAction"
			name="model" input="/welcome.jsp">

			<forward name="success" path="/LogicPresent.jsp" />

		</action>

	</action-mappings>

</struts-config>

LogicPresent.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ taglib uri="/struts-html.tld" prefix="html"%>
<%@ taglib uri="/struts-logic" prefix="logic"%>
<%@ taglib uri="/struts-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body bgcolor="pink">

<h2>Struts - &lt;logic:present&gt; & &lt;logic:notPresent&gt;</h2>

<logic:present name="user">
User object is exists.
</logic:present>
<logic:notPresent name="user">
User object does not exists.
</logic:notPresent>

<br /></br/>
<logic:present name="abc">
Abc object is exists.
</logic:present>
<logic:notPresent name="abc">
Abc object does not exists.
</logic:notPresent>

<br /></br/>

</body>
</html>

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!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 bgcolor="pink">
<html:form action="/LogicTest">
	<p>click <html:submit>here </html:submit> to retrieve details using
	&lt;logic:present&gt;</p>
</html:form>

</body>
</html>

OUTPUT:     

DOWNLOAD+SOURCECODE

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply