UI Tags in Struts

UI Tags in Struts:

Struts 2 UI Tags are straightforward and simple to utilize. You require not compose any HTML code, the UI labels will consequently produce them for you in view of the subject you select. As a matter of course the HTML topic is utilized. The HTML subject uses tables to position the shape components.

In this case you will perceive how to make an enrollment page utilizing Struts 2 UI labels. You will likewise figure out how to prepopulate the shape fields, set default esteems to it and to retrieve the qualities back in the jsp page.

DateTimePicker Example

  • Create index.jsp for input:

This jsp page makes a frame utilizing struts UI labels. It gets name, watchword and email id from the client.

 index.jsp

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

<%@ taglib prefix=”sx” uri=”/struts-dojo-tags” %>

<html>

<head>

<sx:head  />

</head>

<body>

<s:form action=”DateTimePicker” method=”POST”>

<sx:datetimepicker name=”todayDate” label=”Format (yyyy-mm-dd)” displayFormat=”yyyy-MM-dd”/>

<sx:datetimepicker name=”todayDate2″ label=”Format (dd-mm-yyyy)” displayFormat=”dd-MMM-yyyy”/>

<s:submit></s:submit>

</s:form>

</body>

</html>

2) Create the action class

This activity class acquires the ActionSupport class and overrides the execute strategy.

 RegisterAction.java:

package mypack;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;

public  class DateBean  extends ActionSupport {

private Date todayDate,todayDate2;

public Date getTodayDate2() {

return todayDate2;

}

public void setTodayDate2(Date todayDate2) {

this.todayDate2 = todayDate2;

}

public Date getTodayDate() {

return todayDate;

}

public void setTodayDate(Date value) {

todayDate = value;

}

public String execute(){

return SUCCESS;

}

}

 3)Create struts.xml

This xml document characterizes an additional outcome by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN”

“http://struts.apache.org/dtds/struts-2.1.dtd”>

<struts>

<package name=”s” extends=”struts-default”>

<action name=”DateTimePicker” class=”mypack.DateBean”>

<result>/welcome.jsp</result>

</action>

</package>

</struts>

4) Create view component

It is the straightforward jsp document showing the data of the client.

welcome.jsp:

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

first date:<s:property value=”todayDate” />

second date:<s:property value=”todayDate2″ />

Iterator Tag

Struts 2 Iterator tag is used to accentuate over an value which can be any of java.util.Collection or java.util.Iterator. In these instructional activities, you will make an once-over factor, use Iterator tag to circle over it and get the iterator status with IteratorStatus.

1)Create index.jsp for input

 index.jsp

<a href=”fetch”>findPartner</a>

2)Create the action class

Find.java

package mypack;

import java.util.ArrayList;

public class Find {

private ArrayList list=new ArrayList();

public ArrayList getList() {

return list;

}

public void setList(ArrayList list) {

this.list = list;

}

public String execute(){

User u1=new User();

u1.setUserName(“Amit”);

u1.setUserPass(“kumar”);

u1.setEmail(“amit@gmail.com”);

User u2=new User();

u2.setUserName(“Vijay”);

u2.setUserPass(“kumar”);

u2.setEmail(“vijay@gmail.com”);

list.add(u1);

list.add(u2);

return “success”;

}

}

3)Create the model

User.java

package mypack;

public class User {

private String userName,userPass,email;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPass() {

return userPass;

}

public void setUserPass(String userPass) {

this.userPass = userPass;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

4) Create struts.xml

struts.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN”

“http://struts.apache.org/dtds/struts-2.1.dtd”>

<struts>

<package name=”arr” extends=”struts-default”>

<action name=”fetch” class=”mypack.Find” method=”execute”>

<result name=”success”>welcome.jsp</result>

</action>

</package>

</struts>

5) Create view component

welcome.jsp

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

Data is:<br/>

<s:iterator value=”list”>

<fieldset>

<table width=”40%”>

<tr><td><s:property value=”userName”/></td></tr>

<tr><td><s:property value=”userPass”/></td></tr>

<tr><td><s:property value=”email”/></td></tr>

</table>

</fieldset>

</s:iterator>

 Registration:

In this case, we will make an enrollment registration utilizing struts UI labels and store these data into the prophet database. You may utilize other database likewise, for example, mysql, DB2 and so forth as per your necessity. We see the table first that we have to make in the prophet database.

CREATE TABLE  “STRUTSUSER”

(    “NAME” VARCHAR2(4000),

“PASSWORD” VARCHAR2(4000),

“EMAIL” VARCHAR2(4000),

“GENDER” VARCHAR2(4000),

“COUNTRY” VARCHAR2(4000)

)

/

  • Create input page (index.jsp)

index.jsp

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

<s:form action=”register”>

<s:textfield name=”name” label=”UserName”></s:textfield>

<s:password name=”password” label=”Password”></s:password>

<s:textfield name=”email” label=”Email”></s:textfield>

<s:radio list=”{‘male’,’female’}” name=”gender”></s:radio>

<s:select cssStyle=”width:155px;”list=”{‘india’,’pakistan’,’other’,}”

name=”country” label=”Country”></s:select>

<s:submit value=”register”></s:submit>

</s:form>

  • Create the action class

 RegisterAction.java

package com.javaspot;

public class RegisterAction {

private String name,password,email,gender,country;

//setters and getters

public String execute(){

int i=RegisterDao.save(this);

if(i>0){

return “success”;

}

return “error”;

}

}

3) Create the class to store data

 RegisterDao.java

package com.javaspot;

import java.sql.*;

public class RegisterDao {

public static int save(RegisterAction r){

int status=0;

try{

Class.forName(“oracle.jdbc.driver.OracleDriver”);

Connection con=DriverManager.getConnection(

“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”oracle”);

PreparedStatement ps=con.prepareStatement(“insert into strutsuser values(?,?,?,?,?)”);

ps.setString(1,r.getName());

ps.setString(2,r.getPassword());

ps.setString(3,r.getEmail());

ps.setString(4,r.getGender());

ps.setString(5,r.getCountry());

status=ps.executeUpdate();

}catch(Exception e){e.printStackTrace();}

return status;

}

}

4) Map the request in file

struts.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD

Struts Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>

<struts>

<package name=”default” extends=”struts-default”>

<action name=”register” class=”com.javaspot.RegisterAction”>

<result name=”success”>register-success.jsp</result>

<result name=”error”>register-error.jsp</result>

</action>

</package>

</struts>

5) Create view components

register-success.jsp

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

Welcome, <s:property value=”name”></s:property>

register-error.jsp

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

Sorry, some error occured!

<s:include value=”index.jsp”></s:include>

Login and Logout:
Before making the login and logout application utilizing struts 2, you should clear the ideas of  aware interfaces in struts 2. In this illustration, we have utilized the SessionAware interface to put the data in the session extension and ServletActionContext class to get the data from the session scope.
 This illustration contains three connections login, logout and profile. The end client can’t tap on the profile page until he/she is signed in. Subsequent to getting signed in, he/she may go the profile page. On the off chance that the end client taps on the logout page, he won’t have the capacity to get to the profile page.

CREATE TABLE  “USER3333”

(    “ID” NUMBER,

“NAME” VARCHAR2(4000),

“PASSWORD” VARCHAR2(4000),

“EMAIL” VARCHAR2(4000),

CONSTRAINT “USER3333_PK” PRIMARY KEY (“ID”) ENABLE     )  /

Fetching all records:

To get every one of the records, we have put away every one of the records in an accumulation (utilizing List), and showing the information of the gathering utilizing the iterator tag of struts2.

Here, we expect that you have a table in oracle database named user3333 that contains records. The table query is

CREATE TABLE  “USER3333”

(    “ID” NUMBER,

“NAME” VARCHAR2(4000),

“PASSWORD” VARCHAR2(4000),

“EMAIL” VARCHAR2(4000),

CONSTRAINT “USER3333_PK” PRIMARY KEY (“ID”) ENABLE   )  /

To fetch all the records of the table

  1. index.jsp
  2.  Register.java
  3.  User.java
  4.  struts.xml
  5.  welcome.jsp

 1) Create index.jsp for invoking action

index.jsp

<a href=”viewrecords”>View All Records</a>

2) Create the action class

Register.java

package com.javaspot;

import java.sql.*;

import java.util.ArrayList;

public class FetchRecords {

ArrayList<User> list=new ArrayList<User>();

public ArrayList<User> getList() {

return list;

}

public void setList(ArrayList<User> list) {

this.list = list;

}

public String execute(){

try{

Class.forName(“oracle.jdbc.driver.OracleDriver”);

Connection con=DriverManager.getConnection(

“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”oracle”);

PreparedStatement ps=con.prepareStatement(“select * from user3333”);

ResultSet rs=ps.executeQuery();

while(rs.next()){

User user=new User();

user.setId(rs.getInt(1));

user.setName(rs.getString(2));

user.setPassword(rs.getString(3));

user.setEmail(rs.getString(4));

list.add(user);

}

con.close();

}catch(Exception e){e.printStackTrace();}

return “success”;

}

}

  • Create the class to represent table

 User.java

package com.javaspot;

public class User {

private int id;

private String name,password,email;

//getters and setters

}

4) Create struts.xml

struts.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD

Struts Configuration 2.1//EN”

“http://struts.apache.org/dtds/struts-2.1.dtd”>

<struts>

<package name=”anbc” extends=”struts-default”>

<action name=”viewrecords” class=”com.javatpoint.FetchRecords”>

<result name=”success”>displayrecords.jsp</result>

</action>

</package>

</struts>

5) Create view component

welcome.jsp

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

<h3>All Records:</h3>

<s:iterator  value=”list”>

<fieldset>

<s:property value=”id”/><br/>

<s:property value=”name”/><br/>

<s:property value=”password”/><br/>

<s:property value=”email”/><br/>

</fieldset>

</s:iterator

 
Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.