A simple example of adding a JSP page view using the Spring 3 MVC framework

If you follow the tutorial in the References below and checkout the mvc-basic » sample project, there is already a sample view called welcome.

To create a second view, here is what you do.

Create a second jsp page called say, foobar.jsp and put it in src/main/webapp/WEB-INF/views/. The contents can be something like the following,

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><fmt:message key="foobar.title" /></title>
</head>
<body>
<h1><fmt:message key="foobar.title" /></h1>
</body>
</html>

We're using the fmt tag library so that we can store messages in a messages.properties file. This messages.properties file is located at src/main/webapp/WEB-INF/messages/. I added one line to set a title for this foobar.jsp jsp.

welcome.title=Congratulations! Spring is running!
foobar.title=Welcome to Foobar's page!!!
typeMismatch=could not be parsed

The line added starts with foobar.title and can be referenced in the jsp with the <fmt:message /> tag as <fmt:message key="foobar.title" />.

In the src/main/webapp/WEB-INF/spring/mvc-config.xml file, I added one line to map up the new foobar.jsp view as follows,

<mvc:view-controller path="/foobar" view-name="foobar"/>

Basically the jsp file name should be the view-name minus the .jsp extension, and for consistency, the messages.properties key starts with the same followed by .title or whatever message you want for that view. The path was set to /foobar but you could map it to whatever you want.

References

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.