Using The Struts Multibox Tag With LabelValueBeans

March 12, 2007 · Posted in struts · Comment 

The Jakarta Struts <html:multibox> tag can be used to display a group of checkboxes on an HTML form. The tag sets the state of the checkboxes according to the contents of an associated array. The LabelValueBean is a simple class that associates a label with a value. This can be used in combination with the <html:multibox> tag to create checkboxes that return different strings to the ones displayed in the user interface.

The code given below displays four checkboxes with the labels “Alpha”, “Beta”, “Charlie” and “Delta” and the checkboxes return the values “A”, “B”, “C” and “D” respectively.

The JSP:

<logic:iterate name=”myActionForm” id=”item” property=”possibleOptions”>
<html:multibox property=”selectedOptions”>
<bean:write name=”item” property=”value” />
</html:multibox>
<bean:write name=”item” property=”label” /><br />
</logic:iterate>

The Struts Form class:

import org.apache.struts.util.LabelValueBean;

public class MyActionForm extends ActionForm {
private LabelValueBean[] possibleOptions;
private String[] selectedOptions;

public MyActionForm() {
// Initialise the LabelValueBeans in the possibleOptions array.
LabelValueBean[] lvBeans = new LabelValueBean[4];

lvBeans[0] = new LabelValueBean(“Alpha”, “A”);
lvBeans[1] = new LabelValueBean(“Beta”, “B”);
lvBeans[2] = new LabelValueBean(“Charlie”, “C”);
lvBeans[3] = new LabelValueBean(“Delta”, “D”);

this.possibleOptions = lvBeans;
}

public LabelValueBean[] getPossibleOptions() {
return possibleOptions;
}

public String[] getSelectedOptions() {
return selectedOptions;
}

public void setSelectedOptions(String[] selectedOptions) {
this.selectedOptions = selectedOptions;
}
}

Variable name for getters

February 28, 2007 · Posted in java · Comment 

It’s NOT the name of the member variable that counts, it’s the name of the get method.

You use the part after “get” as the property name.

Normally you would convert the first letter after “get” to lowercase, but if the second letter is uppercase also, then you don’t.

getXYAxis – the variable name should be “XYAxis”

getName – the variable name should be “name”

Legal Identifiers in Java

December 13, 2006 · Posted in java · Comment 
  • Identifiers must start with a letter, a currency character($), or an underscore(_).
  • Identifiers cannot start with a number.
  • After the first character, identifiers can contain any combination of letters, currency characters, underscores, or numbers.
  • Identifiers are case-sensitive
  • “enum” cannot be used as identifier because it is added as a Java keyword in 5.0

Heap size and Eclipse

October 29, 2006 · Posted in java · Comment 

To increase heap size of eclipse editor, modify eclipse.ini file to the values you need
-vmargs
-Xms128M
-Xmx512M
-XX:PermSize=64M
-XX:MaxPermSize=128M

Read The Init-Params Defined In The web.xml

August 20, 2006 · Posted in java · Comment 

[eg. of web.xml

<web-app>
<servlet>
<servlet-name>ReadInitParams</servlet-name>
<servlet-class>com.company.MyServlet</servlet-class>
<display-name>Example Servlet</display-name>
<init-param>
<param-name>emailHost</param-name>
<param-value>192.168.1.1</param-value>
</init-param>
</servlet>
</webapp>

In Servlet Code:

public void init(ServletConfig config) throws ServletException {
String emailHost = config.getInitParameter("emailHost");
}

or

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String host = getServletConfig().getInitParameter("emailHost");
}

In JSP Code:

<html>
<head><title>Read Init params from a JSP</title></head>
<body>
<%
String emailHost = null;

public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter("emailHost");
}
%>
<table border="1">
<tr><td>email server</td><td><%=emailHost%></td></tr>
</table>

<% // It can also be read directly from the implicit object - config %>
<%=config.getInitParameter("sys")%><br><br>

</body>
</html>

Difference between request.getParameter() and request.getAttribute()

July 23, 2006 · Posted in java · Comment 

The difference between getAttribute() and getParameter() is that getParameter() will return the value of a parameter that was submitted by an HTML form or that was included in a query string.

getAttribute() returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.

Runtime.getRuntime().exec()

May 5, 2006 · Posted in java · Comment 

To copy a file, you can use the following command:

copy source.txt destination.txt

To run this in Java,

String[] cmd = {“copy”,”source.txt”,”destination.txt”}

Runtime.getRuntime().exec(cmd);

This may generate error=2. To fix this, use the complete path of copy.exe

eg. C:\Windows\System32\copy.exe

Hibernate gives “No CurrentSessionContext configured” error

March 12, 2006 · Posted in hibernate, java · Comment 

If you use Hibernate in a managed environment (with JBoss or some other AS that Hibernate supoorts). Add this line to session-factory section:

<property name=”hibernate.current_session_context_class” >jta </property>

If you use Hibernate in a non-managed environment (standalone application with JDBC). Add this line:

<property name=”hibernate.current_session_context_class” >thread </property>

« Previous Page