Shaun Abram
Technology and Leadership Blog
Handsontable ‘Spreadsheet’ talking to SpringMVC
Handsontable is an Excel-like data grid utilizing JQuery. For example, it provides the ability to copy and paste directly to & from Excel. Handsontable itself if very easy to setup. The part I struggled with was passing the data to SpringMVC. So, this post shows how to send data from a Handsontable data grid on the client to a SpringMVC server.
Tags: excel, handsontable, JavaScript, jQuery, jsp, spreadsheet, spring, springmvc
415 ‘Unsupported Media Type’ error when submitting JSON from a JSP
I’ve been experimenting with submitting JSON data from a JSP to a Spring MVC Controller, using the Spring MVC Ajax example as my guide.
But after setting everything up, I continually ran into this error:
NetworkError: 415 Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method().
After much googling, I found a plethora of solutions, none of which worked for me. In the end, I found it was because I had omitted this line from my JSP:
">
Hope that helps someone else…
Tags: json, jsp, spring, springmvc
Spring MVC Hello World
There is a good Getting Started with Spring MVC blog post over on the Spring team blog.
I have created several Spring MVC projects for both work and play, and am attaching my own simple version of the HelloWorld example here, based on the Spring blog example.
Find my maven ready source here.
Like my previous JSP/Servlet example, I find these templates useful for getting prototypes up and running.
Tags: helloworld, jsp, servlet, spring, springmvc, webapp, webframework
JSP EL statements not being evaluated
On several occasions I have had problems with EL (Expression Language) statements not being evaluated.
As an example, you add a statement to a JSP like
$(2+2), expecting the JSP to simply display 4, when it in fact displays the raw statement, i.e. $(2+2).
After digging around the web, I found a number of points to check…
HelloWorld: JSP -> Servlet
A while ago, I posted a simple HelloWorld Html->Servlet->JSP setup. Even though the html part is unnecessary (a JSP can obviously handle that alone), I deliberately included it as a template for Html/Servlet/JSP interaction. I have used it a bunch of times to get simple Proof of Concept projects up and running, or as a basis to create a simple browser interface to some server side process.
Most of the more recent projects I have used it on however have used straight JSP->Servlet, with no .html file involved. So I am now posting that setup, including some simple JSTL EL and conditional logic.
HelloWorld: Html -> Servlet -> JSP
A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input.
This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of the ubiquitous HelloWorld app. It uses a Html page to receive user input, which is submitted to a Servlet, processed, and the response displayed in a JSP using EL (Expression Language).
Obviously this could be done in various ways, including just JSPs, but this example acts as a template of Html/Servlet/JSP interaction.
1) Create a Html page called HelloWorld.html
Greeting Setup - Hello Who?
2) Create a servlet to receive the user input
HelloWorldServlet.java
package com.helloworld;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//get inputs
String msg = req.getParameter("msg");
//any additional processing can be done here
//set outputs
req.setAttribute("msg", msg);
//forward to jsp
req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res);
}
}
3) Create a JSP to display the response
HelloWorld.jsp:
Hello ${msg}
4) Create the web application’s deployment descriptor file:
web.xml
HelloWorld.html
HelloWorldServlet
com.helloworld.HelloWorldServlet
HelloWorldServlet
/HelloWorldServlet
That’s it.
The easiest way I have found for this kind of development is to create in Eclipse as a Dynamic Web Project (available via the Web Tools Platform that comes with the in the “Eclipse IDE for Java EE Developers” package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse…).
I have attached the source in a war file.