JSF 2.x Conditional error messages

To conditionally show a Twitter Bootstrap styled DIV with a list of error messages, use the rendered property on panelGroup to check whether messageList has messages to be displayed, and if so, render the enclosed content.

layout=”block” displays the output as a DIV, and row is one of the Bootstrap classes for row display.

<div class="span12 alert alert-danger"></div>

Set messages to display from your Backing Bean with:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("your message");

JSTL notes

A few notes for a few common JSTL things so I don’t forget:

taglibs:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Conditionals:

<c:choose>
  <c:when test=””>
  </c:when>
  <!--other when conditions -->
  <c:otherwise>
  </c:otherwise>
</c:choose>

String replace using replace function:

${fn:replace(foo, '"', '\"')}

This next tip for using a loop varStatus to build HTML ids is from: http://stackoverflow.com/questions/6600738/use-jstl-foreach-loops-varstatus-as-an-id

<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
  <div id="divIDNo${theCount}">
  </div>
</c:forEach>

This unexpectedly gives:

<div id="divIDNojavax.servlet.jsp.jstl.core.LoopTagSupport$1Status@5570e2">

To get the actual varStatus id you need to use:

${theCount.index} gives value from 0
${theCount.count} gives value from 1

For example:

<div id="divIDNo${theCount.index}">

 

Useful Java ME 8 Embedded code snippets

Collection of ME 8 Embedded code snippets:

  • Using java.util.TimerTask and Timer – for scheduling periodic tasks (reading a sensor, checking a status etc eg
MyTimerTask myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(myTimerTask, millisBeforeStarting, millisBetweenExecution);
  • Opening a GPIO pin:
this.pin = PeripheralManager.open(this.pinId); 
this.pin.setValue(true); // true = on, false = off
  • Adding an EventListener to a switch:
GPIOPinConfig config = new GPIOPinConfig(this.portId, this.pinId,
    GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,
    GPIOPinConfig.TRIGGER_BOTH_EDGES,
this.pin = PeripheralManager.open(config);
this.pin.setInputListener(this);

… where this implements PinListener and valueChanged() method:

public void valueChanged(PinEvent event) {
    GPIOPin eventPin = event.getPeripheral();
    ...
}
  • Opening an I2C device for input/output:
I2CDeviceConfig config = new I2CDeviceConfig(i2cBus, address, addressSizeBits, serialClock);
myDevice = PeripheralManager.open(config);
  • Read from a UART device:
this.uart = PeripheralManager.open(UART_DEVICE_ID);
this.uart.setBaudRate(9600);

InputStream is = Channels.newInputStream(uart);
this.serialBufferedReader = new BufferedReader(new InputStreamReader(is));
  • Read using Generic Connection Framework:
CommConnection conn = (CommConnection)Connector.open("comm:/dev/ttyAMA0;baudrate=9600");
InputStream inputStream = conn.openInputStream();
this.serialBufferedReader = new BufferedReader(new
    InputStreamReader(inputStream));
  • Open and write to a record store:
this.store = RecordStore.openRecordStore(storeName, true);
byte[] dataBytes = data.getBytes();
recordNum = store.addRecord(dataBytes, 0, dataBytes.length);

Debugging JAXB unmarshalling issues

JAXB appears to fail silently in some cases if the XML it’s attempting to unmarshall to mapped classes doesn’t have the necessary mapped properties.

You can get additional output by adding the following:

-Djaxb.debug=true

– displays information during JAXB initialization

Before you call unmarshall() on your Unmarshaller, call setEventHandler() and add a DefaultValidationEventHandler as follows:

Unmarshaller um = jaxbContext.createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

– this will output additional failure information about missing mappings for xml elements, useful if your mapped class ends up with missing values.