Create JSP file
Posted on: December 2, 2010 at 12:00 AM
In this tutorial you will learn how to create a JSP file in struts2

Create a JSP file

To create a JSP file for struts, Some special (Struts specific) tags are used. And to use those tags in JSP you need to import there tag library. struts-tags is the tag library which contains all the struts specific html tags. This tag is User Interface (UI) tag used to make the form or other html pages. To use this tag you need to write <%@ taglib prefix="s" uri="/struts-tags" %> and also do mapping for StrutsPrepareAndExecuteFilter in web.xml file. as

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

If you not do such type of mapping then it may caused an error The Struts dispatcher cannot be found

A simple login page is give below which illustrates how to use the struts-tags

login.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Tets JSP Page </title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<s:form action="login" method="POST">
<table border=0 align="center">
<tr>
<td><s:actionerror /></td>
<td><s:fielderror /></td>
</tr>

<tr>
<td><s:textfield name="userName" label="User Name"/></td>
</tr>
<tr>
<td><s:password name="password" label="Password"/></td>
</tr>
<tr>
<td><s:submit align="center" value="login"/></td>
</tr>
</table>
</s:form>
<html>

In the login page <s:form action="login" method="POST">  indicates the form. The form action is login, this is the name of the action, which is you will map in the struts.xml file. and on submitting the form will call that (login) action.

the tag  <s:actionerror /> and <s:fielderror /> indicates that if there is any error in form action or field that will be displayed by this tag.

In the tag <s:textfield name="userName" label="User Name"/>, textfield, password indicates the type of the fiels and password respectively. label indicates the name of the label shown on the page indicating the field. s:submit indicates the submit button which submits the form.

finally closing the form using </s:form>

You can learn more about struts-tags on roseindia.net

Or on  struts.apache.org/2.0.14/docs/tag-reference.html

Download this example code

Related Tags for Create JSP file:

Advertisements

Ads

 
Advertisement null

Ads