Ready to start researching Spring, today put the development environment well, then take the first MVC app with the Tutorials (developing a spring framework mvc appliological step-by-step) of Spring! To simplify, I deleted a lot of unnecessary things in the example, leaving only one of the simplest MVC applications.
Development Environment: Windows XP SP1 ENGLISH, Eclipse 3.0, Tomcat 5.0, Spring 1.1RC2
Function: Implement a SpringAppController and a view.jsp, SpringAppController is responsible for receiving a hello.c command, then create Model, rendering View.jsp.
First create a springApp project under Eclipse (you can also use Eclipse, but you have to trouble a little), build the necessary directory structure and file:
Among them, 3 JAR files under web / web-inf / lib / next can be found in Spring's DIST and LIB directories, then we will write a SpringAppController.java first:
import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class SpringappController implements Controller {public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {return new ModelAndView ( "view.jsp", "model", "Hello, world!");} }
The Model created here Controller is just a simple string. In the actual application, the Controller should receive the URL parameters, then access the business layer to get Model.
Then create view.jsp to render the results, use the JSTL tag:
<% @ Page session = "false"%>
<% @ Page session = "false"%> <% @ taglib prefix = "c" URI = "http://java.sun.com/jstl/core"%> <% @ Taglib prefix = "fmt" URI = "http://java.sun.com/jstl/fmt"%>
XML Version = "1.0" encoding = "UTF-8"?>
SpringApp-servlet.xml required for Spring MVC:
XML Version = "1.0" encoding = "UTF-8"?>
XML Version = "1.0"?>
OK! View.jsp shows the value of Model "Hello, World!". If an error occurs, carefully check the desired JAR file and the setting of the Tomcat path.
related resources: