JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Struts BEAN Page Tag ( < bean:page >)

September 30, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

[code lang=”html”]&lt;%@ taglib prefix=&quot;bean&quot; uri=&quot;http://struts.apache.org/tags-bean&quot; %&gt;[/code]

< bean:page >

< bean:page > – It is used to retrieve the value from the Page Context of this current page and make it accessible from current page. Name property of this tag must be one of the application, config, request, response, or session.

Example Code for < bean:page>

In this example we do not need the ActionForm bean.
In this example we do not need the Action class.
No need to configure struts-config.xml file

Create a simple jsp page BeanPageTag.jsp which illustrates the < bean:page> tag by id and property attributes.

[code lang=”html”]
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;html&quot; uri=&quot;http://jakarta.apache.org/struts/tags-html&quot; %&gt;
&lt;%@taglib prefix=&quot;bean&quot; uri=&quot;http://jakarta.apache.org/struts/tags-bean&quot; %&gt;

&lt; html&gt;
&lt; head&gt;

&lt; title&gt; Bean Page Tag &lt;/title&gt;
&lt; head&gt;
&lt; body bgcolor=&quot;#DDDDDD&quot;&gt;
&lt; h1 &gt; bean:page tag example &lt;/h1&gt;

&lt; bean:page id=&quot;variable1&quot; property=&quot;request&quot;&gt;
&lt; bean:page id=&quot;variable2&quot; property=&quot;application&quot;&gt;
&lt; h3&gt; Session:&lt;/h3&gt; &lt; bean:write name=&quot;variable1&quot; /&gt; &lt; br&gt;
&lt; h3&gt; Application:&lt;/h3&gt; &lt; bean:write name=&quot;variable2&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
[/code]

5.Building and running the application
Output

Access page:http://localhost:8084/beanpage/BeanPageTag.jsp

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Define Tag ( < bean:define >)

September 29, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

[code lang=”html”]<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
[/code]

< bean:define >

< bean:define > -It is used to create the new atttribute in the scope specified by he toScope property and a corresponding scripting variable both of which are named by the value of the id attribute.The corresponding value to which this new attribute (and scripting variable) is set are specified via use of exactly one of the following approaches like Specifying a name attribute plus optional property and scope attributes,specifying value attribute or by specifying nested body content.

Example Code for < bean:define>

In this example we do not need the ActionForm bean.

1.Simple Action class BeansDefineAction.java which is a sub class of Action.In this example we call this action class first which defines and loads a bean with respective property name and value and make it available for current page.
BeanDefineAction.java

[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class BeanDefineAction extends org.apache.struts.action.Action {

private final static String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

2.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

[code lang=”xml”]

<?xml version="1.0" encoding="UTF-8" ?>

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

< action-mappings>
< action path="/define" type="com.myapp.struts.BeanDefineAction" validate="false">
< forward name="success" path="/BeanDefineTag.jsp"/>
</action>
</action-mappings>

</struts-config>

[/code]

3.Create another simple Jsp page BeanDefineTag.jspwhich is a output page which displays the .Property name and Property value which are defined on the same page.

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> Bean Define Tag Example</title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:define tag example output </h1>

< h3> Property Name: name </h3> < br>
< bean:define id="profile" property="name" value="Java Struts" toScope="request"/>
< h3> Property Value:</h3>
< bean:write name="profile" />
</body>
</html>

[/code]

4.Change or modify the web.xml file by making the welcome file to index.jsp which contains the forward action to Action class.
index.jsp

[code lang=”html”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="define.do"/>
[/code]

5.Building and running the application
Output

Access page:>http://localhost:8084/beandefine/

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts BEAN Struts Tag ( < bean:struts >)

September 28, 2010 by Krishna Srinivasan Leave a Comment

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

[code lang=”html”]<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>[/code]

&lt ; bean:struts &gt ;

&lt ; bean:struts &gt ; -It is used to retrieve the value of the specified Struts internal configuration object.You must specify exactly one of the formBean, forward, and mapping attributes to select the configuration object to be exposed.

Example Code for <bean:struts>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Since we should have atleast one FormBean to use this tag we specify a form bean without any properties.
BeanStrutsForm.java

[code lang=”java”]
package com.myapp.struts;
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 BeanStrutsForm extends org.apache.struts.action.ActionForm {

public BeanStrutsForm() {
super();

}

}

[/code]

3.Simple Action class BeansStrutsAction.java which is a sub class of Action.In this example we call this action class first which loads the internal configuration object value to specified fields and are made accessible to the current page.
BeanStrutsAction.java

[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class BeanStrutsAction extends org.apache.struts.action.Action {

private final static String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>

<!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 name="BeanStrutsForm" type="com.myapp.struts.BeanStrutsForm"/>
</form-beans>

< action-mappings>
< action name="BeanStrutsForm" path="/BeanStrutsAction" scope="request" type="com.myapp.struts.BeanStrutsAction" validate="false">
< forward name="success" path="/BeanStrutsTag.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

5.Create another simple Jsp page BeanStrutsTag.jspwhich is a output page which displays the loaded information of the internal configuration object value.

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> Struts BeanStruts Output Tag</title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:struts tag example output </h1>

< bean:struts id="actionmapping" mapping="/BeanStrutsAction"/>
< h3> Configuration Object Name:</h3> actionmapping < br>
< h3> Connfiguration Object Value:</h3>
< bean:write name="actionmapping" />
</body>
</html>

[/code]

6.Change or modify the web.xml file by making the welcome file to index.jsp which contains the forward action to Action class.
index.jsp

[code lang=”html”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="BeanStrutsAction.do"/>
[/code]

7.Building and running the application
Output

Access page:http://localhost:8084/beanstruts/

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML Rewrite Tag ( < html:rewrite >)

September 27, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:rewrite >

< html:rewrite > -Renders a request URI based on exactly the same rules as the < html:link> tag does, but without creating the < a> hyperlink.

Example Code for < html:rewrite>

1.Create an Jsp page and name it as rewrite.jsp.It is the Welcome page for a user. In this example a hyperlink is provided to click to redirect to action class.

rewrite.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML Rewrite example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:rewrite example </h1>

< a href="< html:rewrite action="rewrite.do"/>"> Click here…< a>
</body>
</html>

[/code]

2.Simple Action class rewriteaction.java which is a sub class of Action which is used as actionforward to output page.
rewriteaction.java

[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class rewriteaction extends org.apache.struts.action.Action {

private static final String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

3.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action and other information for specific user’s request’s.

[code lang=”xml”]

<?xml version="1.0" encoding="UTF-8" ?>

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

< action-mappings>
< action path="/rewrite" type="com.myapp.struts.rewriteaction" >
< forward name="success" path="/rewriteoutput.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

4.Create another simple Jsp page rewriteoutput.jspwhich is a output page for the user.

[code lang=”xml”]

< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title> Struts HTML Rewrite tag output page< /title>
< /head>
< body bgcolor="DDDDDD">
< h1> Struts Rewrite Output Page < /h1>

< /body>
< /html>
[/code]

5.Building and running the application
Output

Access page:http://localhost:8084/rewrite/
The Output Redirected Page is Displayed

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML Link Tag ( < html:link >)

September 27, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:link >

< html:link > -renders an HTML < a> element URL rewriting will be applied automatically, to maintain session state in the absence of cookies. The content displayed for this hyperlink will be taken from the body of this tag.Compulsory any of the attributes (forward,href,page,action) needs to be specified to redirect from base url.We can pass Parameters to the url by using < paramId>,< paramName > for one parameter and we can pass more than one parameter by using the Map objects which contains the key values pairs which determine the parameter id and parameter value.In this example we pass only Single Parameter.

Example Code for < html:link>.

1.Create an Jsp page and name it as linkpage.jsp.It is the Welcome page for a user.This page contains simple Header and a anchor which provides a link to another page.

linkpage.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML Link example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:link example </h1>

< html:link page="/linkoutput.jsp" paramId="id" paramName="name"/> Click < /html:link>

</body>
</html>
[/code]

2.Simple Action class linkaction.java which is a sub class of Action class used to load the parameter values into the page link.Firstly this action class is called and in this execute method contains an setAttribute() method attributes are added to the request.
linkaction.java

[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class linkaction extends org.apache.struts.action.Action {

private static final String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("name","Java");
return mapping.findForward(SUCCESS);
}
}

[/code]

3.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.In this file no need to specify the form bean as we are not using form bean to store.

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>

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

< action-mappings>
< action path="/link" type="com.myapp.struts.linkaction" >
< forward name="success" path="/linkoutput.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

4.Change or modify the web.xml file by making the welcome file to index.jsp which contains the jsp forward to call action method to load parameter data to the request.
index.jsp

[code lang=”html”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="link.do"/>
[/code]

5.Building and running the application
Output

Access page:http://localhost:8084/link/
The param values are added to the URl.

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML JavaScript Tag ( < html:javascript >)

September 27, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]&lt;%@ taglib prefix=&quot;html&quot; uri=&quot;http://struts.apache.org/tags-html&quot; %&gt;[/code]

< html:javascript >

< html:javascript > -render JavaScript validation based on the validation rules loaded by the ValidatorPlugIn.Based on the formName the set of Validation Rules are generated which should match the formName given in the xml file(validation.xml).The dynamicJavascript and staticJavascript attributes default to true, but if dynamicJavascript is set to true and staticJavascript is set to false then only the dynamic JavaScript will be rendered.

Example Code for < html:javascript>

1.Create an Jsp page and name it as loginpage.jsp.It is the Welcome page for a user. In this example the login form is created and simple validation is performed.This page contains two fields to enter by user username and password.

loginpage.jsp

[code lang=”html”]
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;html&quot; uri=&quot;http://jakarta.apache.org/struts/tags-html&quot; %&gt;
&lt;%@taglib prefix=&quot;bean&quot; uri=&quot;http://jakarta.apache.org/struts/tags-bean&quot; %&gt;

&lt; html&gt;
&lt; head&gt;

&lt; title&gt; HTML Javascript Tag &lt;/title&gt;
&lt; head&gt;
&lt; body bgcolor=&quot;#DDDDDD&quot;&gt;
&lt; h1 &gt; Struts html:javascript tag &lt;/h1&gt;

&lt; html:form action=&quot;/login&quot; onsubmit=&quot;return validateLoginForm(this);&quot; &gt;

Enter name: &lt; html:text property=&quot;name&quot;/&gt;&lt; br&gt;
Enter Password :&lt; html:password property=&quot;password&quot;/&gt;&lt; br&gt;
&lt; html:javascript formName=&quot;LoginForm&quot; /&gt;
&lt; html:submit/&gt;

&lt; /html:form&gt;

&lt;/body&gt;
&lt;/html&gt;

[/code]

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have two properties to hold in tha form bean “name” and “password” .If client validation is successfull then these values get stored in the form.
LoginFormForm.java

[code lang=”java”]
package com.myapp.struts;
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 LoginForm extends org.apache.struts.action.ActionForm {

private String name;
private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public LoginForm() {
super();

}

}

[/code]

3.Simple Action class loginaction.java which is a sub class of Action class which is used to redirect the page upon successfull validation .
loginaction.java

[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class loginaction extends org.apache.struts.action.Action {

private final static String SUCCESS = &quot;success&quot;;

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.Additionally a validator Plug-in is added to enable the client side validation.Plug-in contains the path for both validation.xml and validation-rules.xml.

[code lang=”xml”]
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
&quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;
&quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&gt;

&lt; struts-config&gt;
&lt; form-beans&gt;
&lt; form-bean name=&quot;LoginForm&quot; type=&quot;com.myapp.struts.LoginForm&quot;/&gt;
&lt;/form-beans&gt;

&lt; action-mappings&gt;
&lt; action input=&quot;/loginpage.jsp&quot; name=&quot;LoginForm&quot; path=&quot;/login&quot; scope=&quot;request&quot; type=&quot;com.myapp.struts.loginaction&quot; validate=&quot;true&quot;&gt;
&lt; forward name=&quot;success&quot; path=&quot;/loginsuccess.jsp&quot;/&gt;
&lt;/action&gt;
&lt;/action-mappings&gt;

&lt; plug-in className=&quot;org.apache.struts.validator.ValidatorPlugIn&quot;&gt;
&lt; set-property
property=&quot;pathnames&quot;
value=&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;/&gt;
&lt; /plug-in&gt;

&lt;/struts-config&gt;

[/code]

5.Update the validation.xml which contains the Validations required for a given FormName.

[code lang=”xml”]
&lt; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt; !DOCTYPE form-validation PUBLIC
&quot;-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN&quot;
&quot;http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd&quot;&gt;

&lt; form-validation&gt;
&lt; formset&gt;
&lt; form name=&quot;LoginForm&quot;&gt;
&lt; field property=&quot;name&quot; depends=&quot;required&quot;&gt;
&lt; arg key=&quot;LoginForm.name&quot;/&gt;
&lt; /field&gt;
&lt; field property=&quot;password&quot; depends=&quot;required&quot;&gt;
&lt; arg key=&quot;LoginForm.password&quot;/&gt;

&lt; /field&gt;
&lt; /form&gt;
&lt; /formset&gt;
&lt; /form-validation&gt;
[/code]

6.Add following lines in the Application.Resources file which contains the key values.

[code]
LoginForm.name=Enter User Name
LoginForm.password=Enter Password
[/code]

7.Create another simple Jsp page loginsuccess.jsp which the output page for login success if validation is successfull the controller redirects to this page.This page displays the welcome message along with the user name

[code lang=”html”]
&lt; html&gt;
&lt; head&gt;
&lt; meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt; title&gt;Login Successfull Page&lt; /title&gt;
&lt; /head&gt;
&lt; body bgcolor=&quot;DDDDDD&quot;&gt;
&lt; h1&gt; Login Success&lt; /h1&gt;
&lt; h3&gt;
Welcome &lt; bean:write name=&quot;LoginForm&quot; property=&quot;name&quot;/&gt;
&lt; /h3&gt;
&lt; /body&gt;
&lt; /html&gt;

[/code]

8.Building and running the application
Output

Access page:http://localhost:8084/javascript/
If not validated the javascript alert box with error message is displayed.
If succeccfully validated the login success message along with user name is displayed.

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML OptionsCollection Tag ( < html:optionsCollection >)

September 21, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:optionsCollection >

< html:optionsCollection > -renders a HTML option field.This Tag is valid only if nested inside < select> Tag.The element is used to display data of lists (arrays, collections) inside a select element.This tag operates on a collection of beans, where each bean has a label property and a value property.This tag differs from the < html:options> tag in that it makes more consistent use of the name and property attributes, and allows the collection to be more easily obtained from the enclosing form bean.

Example Code for < html:optionsCollection>

1.Create an Jsp page and name it as example.jsp.It is the Welcome page for a user. In this example the options in drop down are loaded dynamically from Collection countrylist(ArrayList).

example.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML OptionsCollection example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:optionsCollection example </h1>

< html:form action="/country" >

< h1> Select Country:
< html:select name="CountryForm" property="country">
< html:option value="0">–Select Country–< /html:option>
< html:optionsCollection name="CountryForm" property="countryList" label="countryname" value="countryid" />
< /html:select> < /h1>
< html:submit value="save" property="method"/>< html:reset value="Reset"/>

< /html:form>

</body>
</html>
[/code]

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have two properties to hold in tha form bean “countrylist” and “country” of type ArrayList(Collection),String respectively.

CountryForm.java
[code lang=”java”]
package com.myapp.struts;
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 CountryForm extends org.apache.struts.action.ActionForm {

private String country;
private ArrayList countryList;

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public ArrayList getCountryList() {
return countryList;
}

public void setCountryList(ArrayList countryList) {
this.countryList = countryList;
}

public CountryForm() {
super();

}
}

[/code]

3.Simple Action class CountryAction.java which is a sub class of Action class used to load the options values into the dropdown list provided to the user. country class objects are added to the ArrayList countrylist and set to the CountryForm’s setCountryList(). It contains a method called save() which is called when the form is submitted.
CountryAction.java
[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class CountryAction extends org.apache.struts.action.Action {

private final static String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CountryForm formBean=(CountryForm)form;
ArrayList countrylist=new ArrayList();
ArrayList statelist=new ArrayList();
countrylist.add(new country("1","INDIA"));
countrylist.add(new country("2","USA"));
countrylist.add(new country("3","CHINA"));
countrylist.add(new country("4","CANADA"));
formBean.setCountry("");
formBean.setCountryList(countrylist);

return mapping.findForward(SUCCESS);
}

public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

return mapping.findForward(SUCCESS);
}
}

[/code]

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.
[code lang=”xml”]

<?xml version="1.0" encoding="UTF-8" ?>

<!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 name="CountryForm" type="com.myapp.struts.CountryForm"/>
</form-beans>

< action-mappings>
< action input="/example.jsp" name="CountryForm" path="/country" scope="request" type="com.myapp.struts.CountryAction" validate="false">
< forward name="success" path="/example.jsp"/>
</action>
</action-mappings>

</struts-config>

[/code]

5.Create another simple Jsp page country.javawhich is a java class used to create the country objects to add to the list. the attribute propery label and value for the option in drop down list are the countryid and countryname from this object
[code lang=”java”]
public class country {
private String countryid;
private String countryname;
public country(String countryid,String countryname)
{
this.countryid=countryid;
this.countryname=countryname;
}

public String getCountryid() {
return countryid;
}

public void setCountryid(String countryid) {
this.countryid = countryid;
}

public String getCountryname() {
return countryname;
}

public void setCountryname(String countryname) {
this.countryname = countryname;
}

}

[/code]
6.Change or modify the web.xml file by making the welcome file to index.jsp which contains the jsp forward to call action method to load the drop down list data.
index.jsp
[code lang=”html”]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="country.do"/>
[/code]
7.Building and running the application
Output

Access page:http://localhost:8084/optionscollection/

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML Img Tag ( < html:img >)

September 21, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:img >

< html:img > -Renders an HTML < img> element with the image at the specified URL. Like the link tag, URL rewriting will be applied automatically to the value specified in src, page, or action to maintain session state in the absence of cookies. This will allow dynamic generation of an image where the content displayed for this image will be taken from the attributes of this tag.

Example Code for < html:img >

1.Create an Jsp page and name it as img.jsp.To illustrate < html:img> tag we require only one simple jsp page which contains this tag.this tag does not require to be nested in form tag.

img.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML Img example </title>
< head>
< body >
< h1 > Struts html:img example </h1>
< html:img src="images/apache-struts-logo.jpg" alt="Struts Logo" />
</body>
</html>
[/code]
2.Building and running the Jsp page

Output

Access page:http://localhost:8084/img.jsp

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML Options Tag ( < html:options >)

September 20, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:options >

< html:options > -renders a HTML option field.This Tag is valid only if nested inside < select> Tag.The element is used to display data of lists (arrays, collections) inside a select element.

Example Code for < html:options>

1.Create an Jsp page and name it as options.jsp.It is the Welcome page for a user.

options.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML File example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:file example </h1>

< html:form action="/optn" >

< h3> Select Color:
< html:select name="optform" property="colors">
< html:option value="">–Select Color–< /html:option>
< html:options name="optform" property="colorlist" />
< /html:select>< br>
< html:submit value="Submit"/>< html:reset value="Reset"/>
< /h3>
< /html:form>

</body>
</html>
[/code]

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have three property to hold in tha form bean “colorlist”,”message” and “colors” of type ArrayList(Collection),Strings respectively.
optform.java
[code lang=”java”]
package com.myapp.struts;
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 optform extends org.apache.struts.action.ActionForm {
private String message;
private String colors;
ArrayList colorlist=new ArrayList();

public String getColors() {
return colors;
}

public void setColors(String colors) {
this.colors = colors;
}

public ArrayList getColorlist() {

ArrayList list=new ArrayList();
list.add(0,"Red");
list.add(1,"Blue");
list.add(2,"Green");
return list;
}

public void setColorlist(ArrayList colorlist) {
this.colorlist = colorlist;
}

public String getMessage() {
return message;
}

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

public optform() {
super();

}
}

[/code]

3.Simple Action class optaction.java which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the file input field and set their respective error message. The Message font color is set to the selected color from the drop down list from user.
optaction.java
[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class optaction extends org.apache.struts.action.Action {

/* forward name="success" path="" */
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
optform formBean=(optform)form;

if(formBean.getColors().equals(""))
{
formBean.setMessage("No Color is Selected from list");
return mapping.findForward(SUCCESS);
}

formBean.setMessage("&lt; span style=’color:"+formBean.getColors()+"’&gt; U selected "+formBean.getColors()+" Color &lt;/span&gt;");
return mapping.findForward(SUCCESS);
}
}

[/code]
4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.
[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>

<!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 name="optform" type="com.myapp.struts.optform"/>
</form-beans>

< action-mappings>
< action name="optform" path="/optn" scope="request" type="com.myapp.struts.optaction" validate="false">
< forward name="success" path="/output.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

5.Create another simple Jsp page output.jspwhich is for displaying the output Message.If nothing is selected from the drop down list sets the respective error message and get displayed .If The color is selected from the drop down the message is set to display the message in selected font color in this page.
[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title> Struts HTML Output Page </title>
</head>
< body bgcolor="#DDDDDD">
< h1> < bean:write name="optform" property="message" filter="false"/> </h1>
</body>
</html>
[/code]

6.Building and running the application
Output
Access page:http://localhost:8084/options/

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

Struts HTML File Tag ( < html:file > )

September 19, 2010 by Krishna Srinivasan Leave a Comment

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

[code lang=”html”]<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>[/code]

< html:file >

< html:file > -renders a file input field.Property should be the corresponding FormFile type.As with the corresponding HTML < input> element, the enclosing form element must specify “POST” for the method attribute, and “multipart/form-data” for the enctype attribute.

Example Code for < html:file >

1.Create an Jsp page and name it as file.jsp.It is the Welcome page for a user.

file.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> HTML File example </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > Struts html:file example </h1>

< html:form action="/filepath" method="POST" enctype="multipart/form-data">

< h3> Please Enter File Name < /h3>
< h3> < bean:write name="fileform" filter="false" property="error"/> </h3>
File Name: < html:file property="file"/> < br> < br>
< html:submit value="Submit"/>
< html:reset value="Reset"/>
</html:form>
</body>
</html>

[/code]

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have one property to hold in tha form bean “file” of type FormFile which belongs to “org.apache.struts.upload” package to hold the file specified in the file input field.
fileform.java
[code lang=”java”]
package com.myapp.struts;
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 fileform extends org.apache.struts.action.ActionForm {
private FormFile file;
private String contenttype;
private int size;
private String fname;
private String error;

public String getContenttype() {
return contenttype;
}

public void setContenttype(String contenttype) {
this.contenttype = contenttype;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public fileform() {
super();

}

}

[/code]

3.Simple Action class formaction.java which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the file input field and set their respective error message.
fileaction.java
[code lang=”java”]
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class formaction extends org.apache.struts.action.Action {

/* forward name="success" path="" */
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
fileform formBean=(fileform)form;
FormFile myFile=formBean.getFile();
String fname=myFile.getFileName();
String ctype=myFile.getContentType();
int size=myFile.getFileSize();
if(fname.equals("") &amp;&amp; size==0)
{
formBean.setError("&lt; span style=’color:red’&gt; Enter any File Name&lt;/span&gt;");
return mapping.findForward(FAILURE);
}
formBean.setContenttype(ctype);
formBean.setFname(fname);
formBean.setSize(size);
return mapping.findForward(SUCCESS);
}
}
[/code]

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.
[code lang=”java”]
<?xml version="1.0" encoding="UTF-8" ?>

<!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 name="fileform" type="com.myapp.struts.fileform"/>
</form-beans>

< action-mappings>
< action name="fileform" path="/filepath" scope="request" type="com.myapp.struts.formaction" validate="false">
< forward name="success" path="/uploadsuccess.jsp"/>
< forward name="failure" path="/file.jsp"/>
</action>
</action-mappings>

</struts-config>
[/code]

5.Create another simple Jsp page uploadsuccess.jspwhich is for displaying the output Message.If file input field is empty and by checking size and file name error message is displayed on the same page itself.
[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title> Struts File Tag Upload success File </title>
</head>
< body bgcolor="#DDDDDD">
< h1> File Uploaded Successfully… </h1>
< h3> Uploaded File Details are: </h3>
< h4> File Name:< span style="color:#0055BB"> < bean:write name="fileform" property="fname" filter="false"/> < /span>< /h4>
< h4> File Content Type:< span style="color:#0055BB"> < bean:write name="fileform" property="contenttype" filter="false"/> < /span>< /h4>
< h4> File Size: < span style="color:#0055BB"> < bean:write name="fileform" property="size" filter="false"/> bytes< /span>< /h4>
</body>
</html>
[/code]

6.Building and running the application
Output

Access page:http://localhost:8084/file/

Struts Framework Articles

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

  • « Previous Page
  • 1
  • 2
  • 3
  • Next Page »

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved