Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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 Example1)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" %>/>

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s[/embed]2) Create the action classThis 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.xmlThis xml document characterizes an additional outcome by the name input, and an interceptor jsonValidatorWorkflowStack.struts.xml version="1.0" encoding="UTF-8" ?>> name="s" extends="struts-default"> name="DateTimePicker" class="mypack.DateBean">/welcome.jsp4) Create view componentIt is the straightforward jsp document showing the data of the client.welcome.jsp:<%@ taglib prefix="s" uri="/struts-tags" %>first date: value="todayDate" />second date: 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.jspfindPartner2)Create the action classFind.javapackage 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 modelUser.javapackage 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.xmlstruts.xmlwelcome.jsp>
5) Create view componentwelcome.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Data is:
 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))/1)Create input page (index.jsp)index.jsp<%@ taglib uri="/struts-tags" prefix="s" %>2)Create the action class RegisterAction.javapackage com.javaspot;public class RegisterAction {private String name,password,email,gender,country;//setters and getterspublic String execute(){int i=RegisterDao.save(this);if(i>0){return "success";}return "error";}}3) Create the class to store data RegisterDao.javapackage 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 filestruts.xml<package name="default" extends="struts-default">class
="com.javaspot.RegisterAction">register-success.jspregister-error.jsppackage>5) Create view componentsregister-success.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Welcome, register-error.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Sorry, some error occured!
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.jspView All Records2) Create the action classRegister.javapackage com.javaspot;import java.sql.*;import java.util.ArrayList;public class FetchRecords {ArrayList list=new ArrayList();public ArrayList getList() {return list;}public void setList(ArrayList 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";}}3)Create the class to represent table User.javapackage com.javaspot;public class User {private int id;private String name,password,email;//getters and setters}4) Create struts.xmlstruts.xml<package name="anbc" extends="struts-default">class="com.javatpoint.FetchRecords">displayrecords.jsppackage>5) Create view componentwelcome.jsp<%@ taglib uri="/struts-tags" prefix="s" %>

All Records: