2, development and configuration applications
(13) Improve index.jsp
l We want to use JSTL in JSP, so import lib / j2ee / jstl.jar and lib / jakarta-taglibs / standard.jar into the SpringApp / Web-INF / LIB directory
l Create include.jsp contains references to Taglibs so that other JSPs contain quotes:
<% @ Page session = "false"%>
<% @ taglib prefix = "c" uri = "http://java.sun.com/jstl/core"%>
<% @ taglib prefix = "FMT" URI = "http://java.sun.com/jstl/fmt"%>
l For these files that do not allow users to directly access them directly, you can put files in / web-infirectory, where you put files in / web-inf / jsp directory
l Modify index.jsp, use the
<% @ include file = "/ web-inf / jsp / incrude.jsp"%>
<% - Redirected Because We can't set the welcome page to a Virtual URL. -%>
(14) Improve view and controller
L, like include.jsp, hello.jsp also cannot access directly, so move hello.jsp into the / web-inf / jsp directory
l In Hello.jsp, we increase the output of the current date and event, which is obtained from the MODEL mentioned later.
<% @ include file = "/ web-inf / jsp / incrude.jsp"%>
Greetings, IT is now
p>
body>
html>
l Since the path change of Hello.jsp, it is necessary to modify the controller to adjust the path to the output view and provide Model data.
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;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory; Public Class Springappcontroller Implements Controller {
/ ** Logger for this class and subclasses * /
Protected final log logger = logfactory.getlog (getclass ());
Public ModlandView HandleRequest (httpservletRequest Request, HttpservletResponse Response)
Throws servletexception, ioException {
String now = (New java.util.date ()). TOSTRING ();
Logger.info ("Returning Hello View with" Now);
Return New ModelandView ("/ Web-INF / JSP / HELLO.JSP", "now", now
}
}
l Enter http: // localhost: 8888 / SPRINGAPP in your browser, you will show the welcome page with the current date and time
(15) Reduce the coupling of views and controllers
l The above example is dependent on the view and controller. Our idea is to use logical names to map views. This allows you to modify the controller when changing the view (such as name, path).
l Simple mapping is to set the prefix and suffix using the InternalResourceViewResolver
l Modify springapp-servlet.xml to add ViewResolver entry
XML Version = "1.0" encoding = "UTF-8"?>
- Application Context Definition for "SpringApp" DispatcherServlet.
->
bean>
beans>
l You can remove the prefix and suffix of the view in the controller.
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;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public Class SpringAppController Implements Controller {
/ ** Logger for this class and subclasses * /
Protected final log logger = logfactory.getlog (getclass ());
Public ModlandView HandleRequest (httpservletRequest Request, HttpservletResponse Response)
Throws servletexception, ioException {
String now = (New java.util.date ()). TOSTRING ();
Logger.info ("Returning Hello View with" Now);
Return New ModelandView ("Hello", "NOW", NOW;
}
}
(16) Add business logic
l Product class provides business data, the ProductManager class is used to manage Product (ie business data); they are all implemented as Javabean
Package bus;
Import java.io.serializable;
Public Class Product Implements Serializable {
Private string description;
PRIVATE DOUBLE PRICE;
Public void setdescription (String s) {
Description = S;
}
Public string getdescription () {
Return description;
}
Public void setprice (double d) {
Price = d;
}
Public double getprice () {
Return Price;
}
}
Package bus;
Import java.io.serializable; import java.util.list;
Public Class ProductManager IMPLEments Serializable {
PRIVATE LIST PRODUCTS;
Public void setProducts (List P) {
Products = P;
}
Public List getProducts () {
Return Products;
}
}
l In order to separate web representations and business logic, these classes and controllers are placed in different packages, BUS and web; pay attention to move SpringAppController to the web package
l Modify SpringAppController to save the product of the ProductManager to deliver business data to the view; because multiple objects need to be transmitted, use the MAP structure
Package web;
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;
Import java.util.map;
Import java.util.hashmap;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Import Bus.ProductManager;
Public Class SpringAppController Implements Controller {
/ ** Logger for this class and subclasses * /
Protected final log logger = logfactory.getlog (getclass ());
Private productManager product;
Public ModlandView HandleRequest (httpservletRequest Request, HttpservletResponse Response)
Throws servletexception, ioException {
String now = (New java.util.date ()). TOSTRING ();
Logger.info ("Returning Hello View with" Now);
Map mymodel = new hashmap ();
MyModel.Put ("now", now);
MyModel.put ("Products", getProductManager (). getProducts ());
Return New ModelandView ("Hello", "Model", MyModel;
}
Public Void SetProductManager (ProductManager PM) {
PRODMAN = PM;
}
Public productManager getProductManager () {
Return Prodman
}
}
(17) Modify the view to display business data, and provide message beam support
l Use
p>
c: foreach>
body>
html>
(18) Increase test data to automatically assemble business objects
l The above example has not added a function code from the database to load the business object, so use Spring's bean and application context functionality to instantiate, this needs to add some Bean entry in SpringApp-servlet.xml
l Add Messagesource Specify Message Resources Save File Messages.Properties
XML Version = "1.0" encoding = "UTF-8"?>
- Application Context Definition for "SpringApp" DispatcherServlet.
->
Property>
bean>
Property>
bean>
bean>
bean>
bean>
bean>
bean>
beans>
(19) Add a message bundle
l Create a Messages.properties file in the SpringApp / Web-INF / CLASSES directory, pay attention to the consistency of the Key value and
Title = SpringApp
Heading = hello :: SpringApp
Greeting = Greetings, IT IS NOW
l Red deploy, enter http: // localhost: 8888 / SpringApp in your browser, the page with current date and time and business data will be displayed