Creates a
series of checkboxes from a list.
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: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; 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; } @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("]"); 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
http://localhost:8080/struts_tutorial/formEx.jsp
Once I
submit above form, I got following output in console.
FormAction
[userName=Hari Krishna, password=abcdef, code=1, description=Nothing much,
movies=true, cricket=false, coding=false, yourColor=[red, green, yellow]]
No comments:
Post a Comment