Gathered words from an unknown geek. Based on observations and experiences, words
are born. Presented in forms of poems, essays and codes.
A silent lad transformed into a talkative chap in this hidden World Of Mine.
[Java + Struts 2 + Log4j]: Simple Multi Selections Checkbox
Differ from the usual, this tutorial will be about Java and its related frameworks, i.e Struts 2 and Log4j. I’ve been searching over the internet for something similar, but failed. Hence I have to explore myself. Finally, here it is, I’m sharing it with rest of the world.
The most important component here is Java and Struts 2, log4j is only for logging purpose, you may choose to use System.out.print() as well. Let’s begin.
We start off by configuring the file struts.xml. Add the following actions inside the package:
-
-
<action name="checkBoxView" class="com.mohdrashidi.blog.sources.Checkbox">
-
<result name="input">/jsp/checkBox.jsp</result>
-
<result>/jsp/checkBox.jsp</result>
-
</action>
-
Then, the place where actions are being handled, the java class (class name: Checkbox):
-
-
package com.mohdrashidi.blog.sources;
-
-
import com.opensymphony.xwork2.ActionSupport;
-
import java.util.List;
-
import org.apache.log4j.Logger;
-
-
/**
-
*
-
* @author Mohd Rashidi Bin Mohd Zin
-
*/
-
-
private List<String> states;
-
-
if ((getStates() != null) && !getStates().isEmpty()) {
-
-
log.info("Current State: "+state);
-
}
-
}
-
-
return SUCCESS;
-
}
-
-
public List<String> getStates() {
-
return states;
-
}
-
-
public void setStates(List<String> states) {
-
this.states = states;
-
}
-
}
-
As you can see in the Java class, I chose to use Logger to print out selected states. As being mentioned earlier, you may use System.out as well.
Finally is the file of which contains the struts 2 tags (file name: checkBox.jsp):
-
-
<%–
-
Document : checkBox
-
Created on : Jul 3, 2008, 7:54:29 PM
-
Author : shidi
-
–%>
-
-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
-
<%@taglib prefix="s" uri="/struts-tags" %>
-
"http://www.w3.org/TR/html4/loose.dtd">
-
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>Struts 2 Checkbox POC</title>
-
</head>
-
<body>
-
<s:form method="post" action="checkBoxView.action">
-
<s:checkbox label="KL" fieldValue="kl" name="states" />
-
<s:checkbox label="Selangor" fieldValue="sel" name="states" />
-
<s:checkbox label="Kedah" fieldValue="ked" name="states" />
-
<s:checkbox label="Perak" fieldValue="per" name="states" />
-
<s:checkbox label="Kelantan" fieldValue="kel" name="states" />
-
<br><br>
-
<s:submit value="Select States" name="submit" />
-
</s:form>
-
</body>
-
</html>
-
Basically this is just a proof of concept. You will see that once user submitted their selection, those selections will be display at the back end. In this case, in form of logging. While the page will be refresh and you will see that all check boxes are being checked, this is most likely happens because we are using same name of all check boxes.
That’s all geeks. Happy coding ![]()
Tags: checkbox, java, log4j, struts 2







tx bro!

amat2 membantu kuuuuuuuuuuuuuuuuuu
leh aaa blanje kamu kopi ais satuuuu!
Where is the best location for log4j.xml file ?
I have problem with this. Thk