Struts 2 Control tages

1. if-elseif-else tags: These are the basic control tags used for conditional logic in result pages. elseif and else tags always work with if tags. if and elseif tags have test attribute where we can provide expression that should result in boolean values, true or false. Example usage is:-

Create the dynamic web project from new project in elipse and then create the following files:-

 

    Step 1:- Create view in web content dir named Index.jsp

 

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

pageEncoding="ISO-8859-1"%>

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

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Strutd 2 Tags Exampletitle>

head>

<body>

<h3>Struts2 Tagesh3>

<form action="hello">

<label for="name">Select a namelabel><br/>

<select name="name">

<option name="Amit">Amitoption>

<option name="Alok">Alokoption>

<option name="Amardeep">Amardeepoption>

select>

<input type="submit" value="Say Hello"/>

form>

body>

html>

 

Step2:- create HelloWorldAction.java in your package. like r4r package is here u can use any name for package.

 

package r4r;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private String name;

public String execute() throws Exception {

return "success";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

 

Step3 :- Now create on e welcome page HelloWorld.jsp

 

<%@ page contentType="text/html; charset=UTF-8" %>

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

<html>

<head>

<title>Example for If and Else tagtitle>

head>

<body>

<b>Example for If and Else tagb><br/>

<s:if test="name=='Alok'">

You have selected 'Alok'.

s:if>

<s:elseif test="name=='Amit'">

You have selected 'Amit'

s:elseif>

<s:elseif test="name=='Amardeep'">

You have selected 'Amardeep'

s:elseif>

<s:else>

You have not selected 'Mike' or 'Jason' or 'Amardeep'.

s:else>

body>

html>

 

Step4 :- Now create struts.xml

 

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

DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="helloworld" extends="struts-default">

<action name="hello"

class="r4r.HelloWorldAction"

method="execute">

<result name="success">/HelloWorld.jspresult>

action>

package>

struts>

 

Step4 :- Now create web.xml

 

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>Logindisplay-name>

<welcome-file-list>

<welcome-file>index.jspwelcome-file>

welcome-file-list>

<filter>

<filter-name>struts2filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

filter-class>

filter>

<filter-mapping>

<filter-name>struts2filter-name>

<url-pattern>/*url-pattern>

filter-mapping>

web-app>

 

You can download this application  from here

 

 

 

 

2. Append :- This tag take two or more lists as parameters and append them all together as shown below:

 

 Example usage is:-

 

<s:append var="myAppendIterator">

<s:param value="%{myList1}" />

<s:param value="%{myList2}" />

<s:param value="%{myList3}" />

</s:append>

<s:iterator value="%{#myAppendIterator}">

<s:property />

</s:iterator>

 

Create the dynamic web project from new project in elipse and then create the following files:-

 

 Step 1:- Create view in web content dir named Index.jsp

 

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

pageEncoding="ISO-8859-1"%>

<!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>Insert title here</title>

</head>

<body>

<h1>hello</h1>

<a href="student.jsp">click heare...</a>

</body>

</html>

 

Step 2:- Create student.jsp

 

<%@ page contentType="text/html; charset=UTF-8"%>

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

<html>

<head>

<title>Employees</title>

</head>

<body>

<b>Employees and Contractors Merged together</b>

<br />

<s:append id="allstudents">

<s:param value="students" />

<s:param value="cources" />

</s:append >

<s:iterator value="allstudents">

<s:property value="name"/>,

<s:property value="cource"/><br/>

</s:iterator>

</body>

</html>

 

 

Step 3:- Create Student.java

 

package r4r;

import com.opensymphony.xwork2.ActionSupport;

import java.util.*;

public class Student extends ActionSupport {

static final long serialVersionUID = 1L;

private String name;

private String cource;

private List<Student> students;

private List<Student> cources;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCourse() {

return cource;

}

public void setCourse(String course) {

this.cource = course;

}

public List<Student> getStudents() {

return students;

}

public void setStudents(List<Student> students) {

this.students = students;

}

public List<Student> getCources() {

return cources;

}

public void setCources(List<Student> cources) {

this.cources = cources;

}

public Student(){}

public Student(String name, String course){

this.name=name;

this.cource=course;

}

@Override

public String execute() throws Exception {

students = new ArrayList<Student>();

students.add(new Student("George","Recruitment"));

students.add(new Student("Danielle","Accounts"));

students.add(new Student("Melissa","Recruitment"));

students.add(new Student("Rose","Accounts"));

cources = new ArrayList<Student>();

cources.add(new Student("Mindy","Database"));

cources.add(new Student("Vanessa","Network"));

return "success";

}

}

 

Step 4:- Create struts.xml

 

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

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="r4r" extends="struts-default" namespace="/">

<action name="Student"

class="r4r.Student"

method="execute">

<result name="success">/student.jsp</result>

</action>

</package>

</struts>

 

you can download the project from here