Wednesday 13 July 2016

STRUTS 2 UI TAGS and CONTROL TAGS

Combo Box tag in Struts 2

 <s:select name="Dept" label="Department" headerKey="0" headerValue="Select" list="{'CSE','IT','ECE'}" />

If else Condition using Struts 2

<s:if test=condition>
   //statement
</s:if>
<s:elseif test=condition>
   //statement
</s:elseif>
<s:else>
   //statement
</s:else>


EXAMPLE FOR CONTROL TAGS

home.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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Combo Box Example</title>
</head>
<body>
<s:form action="Condition">
 <s:select name="Dept" label="Department" headerKey="0" headerValue="Select" list="{'CSE','IT','ECE'}" />
 <s:submit label="submit"/>
</s:form>
</body>
</html>

Create xml files


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="Condition" class="com.jeni.Check">
<result name="success">conditional.jsp</result>
</action>  </package> </struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>StrutsTags</display-name>
  <filter>
  <filter-name>Struts 2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
  </filter>
   <filter-mapping>
  <filter-name>Struts 2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 Create Action class:  Check.java

package com.jeni;

public class Check {
private String dept;

public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;}
public String execute()
{
return "success";
}}

Conditional.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Example of If and Else</title>
</head>
<body>
<br />
<s:if test="dept=='CSE'">
   You liked the CSE
</s:if>
<s:elseif test="dept=='IT'">
   You liked the IT
</s:elseif>
<s:else>
   You liked ECE
</s:else>
</body>
</html>



No comments:

Post a Comment