DaedTech

Stories about Software

By

JSTL Core ForEach Loop

Today, I was pleasantly surprised at how easy a time I had setting up some JSP pages to interact with my ongoing Java/Spring MVC home automation server. I seem to remember the setup for this being annoying in a past Java life, but my experience today was the opposite. So, here is a brief summary of what I did.

My plan is to install MongoDB to store the data that I’m going to use. I don’t know if this is the right choice, but it seems like a lightweight one in that I can always go “heavier” with a RDBMS later, if that seems warranted. There’s also a bit of a “let’s try it out” motivation for me in that I can add another tool to my toolbox in the process. But, that’s a task for another time (and probably another post). For now, I’m going to mimic having a persistence structure with the following java class:

public class HouseServiceBasicImpl implements HouseService {

	@Override
	public House getHouse() {
		// TODO replace with actual implementation
		List myRoomList = Arrays.asList("Kitchen", "Dining Rooom", "Bedroom"); 
		return new HouseModel(myRoomList);
	}
}

The House interface currently contains only a collection of room names. Here is the controller that uses the service:

@RequestMapping("/rooms")
public class HouseRoomController {

	private HouseService _houseService;
	
	/**
	 * Dependency injected constructor
	 * @param service
	 */
	public HouseRoomController(HouseService service) {
		if(service == null)
			throw new IllegalArgumentException("service");
		
		_houseService = service;
	}

	@RequestMapping("/rooms")
	public ModelAndView rooms() {

		//@TODO get rid of the law of demeter violation here - this should just pass the house to the model and view
		ModelAndView myModelAndView = new ModelAndView();
		Collection myRoomNames = _houseService.getHouse() == null ? new ArrayList() : _houseService.getHouse().getRoomNames();
		myModelAndView.addObject("rooms", myRoomNames);
		
		return myModelAndView;
	}
}

All we do here is pull the list of rooms from the service, and add them as a model to a ModelAndView with key “rooms”. With that in place, I wrote the following jsp page:

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




		

From there, I wired up my spring-servlet.xml with all the proper beans and tried to build, only to get an error: “The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved.” After some mild poking around on google, I learned that I didn’t have “standard.jar” in my WEB-INF\lib folder, and I needed it there. I found the jar at this site, downloaded it, and stuck it in that folder, and I was able to build.

From there, I ran the app on the server, and got an exception, though this was easy to track down. I had omitted an equals somewhere. Once I corrected that, I saw my page with my three rooms, as expected. And, I was pretty pleased.

I don’t know if it’s that I’m a lot more experienced at programming in general, or if Spring/Java/Eclipse has gotten easier to setup and use, but everything is much easier for me this time around than I remember from some years back. I suspect it’s a little from column A and a little from column B. One of the things that the .NET world has always seemed to have on the Java world is that everything is easier to setup and hit the ground running with. It seems as though the Java community is making noticeable strides in this department.