Saturday 24 October 2015

Struts2 : opt group tag example

Create a optgroup component which needs to resides within a select tag. This component is to be used within a Select component.

Following snippet shows optgroup example.

<s:select label="Best place you like" name="myBestPlace" list="%{#{}}"
 headerValue="Select your place" headerKey="No place selected">
 <s:optgroup label="INDIA"
  list="%{#{'BANGALORE':'Bangalore', 'VIJAYAWADA':'Vijayawada'}}" />
 <s:optgroup label="CHINA"
  list="%{#{'BIEJING':'Biejing', 'SHANGHAI':'Shanghai', 'GUILIN':'Guilin'}}" />
 <s:optgroup label="RUSSIA"
  list="%{#{'MOSCOW':'Moscow', 'KAZAN':'Kazan', 'SOCHI':'Sochi'}}" />
</s:select>


formEx.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <s:form action="formAction" method="post">
  <s:label> Simple form example</s:label>
  <s:textfield name="userName" label="user name : " />
  <s:password name="password" label="password : " />
  <s:hidden name="code" value="1" />
  <s:textarea name="description" rows="10" cols="50"
   label="Tell us about yourself" />

  <s:label> Your Hobbies </s:label>
  <s:checkbox name="movies" label="Movies" />
  <s:checkbox name="cricket" label="Cricket" />
  <s:checkbox name="coding" label="Coding" value="true" disabled="true" />
  <s:checkboxlist label="What's your favor color"
   list="#{'red':'RED','blue':'BLUE','green':'GREEN','yellow':'YELLOW'}"
   name="yourColor" />

  <s:select label="Country" headerKey="No country selected"
   headerValue="Select Your country"
   list="#{'IN':'INDIA','BAN':'BANGLADESH','AUS':'AUSTRALIA','USA':'AMERICA'}"
   name="country" />

  <s:select label="Best place you like" name="myBestPlace" list="%{#{}}"
   headerValue="Select your place" headerKey="No place selected">
   <s:optgroup label="INDIA"
    list="%{#{'BANGALORE':'Bangalore', 'VIJAYAWADA':'Vijayawada'}}" />
   <s:optgroup label="CHINA"
    list="%{#{'BIEJING':'Biejing', 'SHANGHAI':'Shanghai', 'GUILIN':'Guilin'}}" />
   <s:optgroup label="RUSSIA"
    list="%{#{'MOSCOW':'Moscow', 'KAZAN':'Kazan', 'SOCHI':'Sochi'}}" />
  </s:select>

  <s:submit />
 </s:form>
</body>
</html>

success.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <h1>Successfull</h1>
</body>
</html>


FormAction.java

package strutstutorial.actions;

import java.util.List;

public class FormAction {
 private String userName;
 private String password;
 private int code;
 private String description;
 private boolean movies;
 private boolean cricket;
 private boolean coding;
 private List<String> yourColor;
 private String country;
 private String myBestPlace;

 public String execute() {
  System.out.println(this);
  return "success";

 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getCode() {
  return code;
 }

 public void setCode(int code) {
  this.code = code;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 public boolean getMovies() {
  return movies;
 }

 public void setMovies(boolean movies) {
  this.movies = movies;
 }

 public boolean getCricket() {
  return cricket;
 }

 public void setCricket(boolean cricket) {
  this.cricket = cricket;
 }

 public boolean getCoding() {
  return coding;
 }

 public void setCoding(boolean coding) {
  this.coding = coding;
 }

 public List<String> getYourColor() {
  return yourColor;
 }

 public void setYourColor(List<String> yourColor) {
  this.yourColor = yourColor;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getMyBestPlace() {
  return myBestPlace;
 }

 public void setMyBestPlace(String myBestPlace) {
  this.myBestPlace = myBestPlace;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("FormAction [userName=").append(userName)
    .append(", password=").append(password).append(", code=")
    .append(code).append(", description=").append(description)
    .append(", movies=").append(movies).append(", cricket=")
    .append(cricket).append(", coding=").append(coding)
    .append(", yourColor=").append(yourColor).append(", country=")
    .append(country).append(", myBestPlace=").append(myBestPlace)
    .append("]");
  return builder.toString();
 }

}


struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

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

  <action name="formAction" class="strutstutorial.actions.FormAction">
   <result name="success">/success.jsp</result>
  </action>

 </package>
</struts>


Demo
Once I submit above form, I got following data in console.

FormAction [userName=Hari Krishna, password=password123, code=1, description=Nothing much, movies=true, cricket=false, coding=false, yourColor=[green, yellow], country=IN, myBestPlace=SHANGHAI]




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment