Spring start example "developing a spring framework mvc application step-by-step" analysis (2)

xiaoxiao2021-03-06  69

3, reduce Controler's dependence on page address

In the controler above, Return New ModelandView ("Web-INF / JSP / HELLO.JSP", "NOW", NOW; points out the absolute path of the JSP page, which can be changed.

First add a bean definition in SpringApp-servlet.xml:

org.springframework.web.servlet.view.jstlview

/ web-inf / jsp /

.jsp

Then write the Controler back to:

Return New ModelandView ("Hello", "NOW", NOW;

Such internalResourceViewResolver will automatically add Prefix and SUFFIX before returning to the page.

(... I don't know what is used. If my path has multiple, what should I add? ... it seems that I have to put it under the JSP)

4, bean definitions to define their properties into other beans

In SpringApp-servlet.xml, we make the following definitions

Lamp

5.75

Table

75.25

chair

22.79

There are 2 points to understand:

1, you can assign a value directly using 75.25 Assign the property as another defined bean.

2. These beans defined as attributes are loaded and assigned at the same time when Web Module is loaded. Once loaded, you can use it directly, so it is not necessary to assign these attributes for these attributes in the Class definition (but must have the setter definition of these attributes). For example, according to the above XML definition, we write SpringAppController to this:

Public Class SpringAppController Implements Controller {

Private productManager product;

Public ModlandView HandleRequest (httpservletRequest Request, httpservletResponse response) throws servletexception, ioException {

Map mymodel = new hashmap ();

MyModel.put ("Products", getProductManager (). getProducts ());

Return New ModelandView ("Hello", "Model", MyModel;

}

Public Void SetProductManager (ProductManager PM) {

PRODMAN = PM;

}

Public productManager getProductManager () {

Return Prodman

}

}

It can be seen that there is no value for the property Private ProductManager ProductMan, but it defines its setter - setProductManager, which is automatically called when is loaded to give the product. 5, unit test

In Web application development, our test method is actual test, that is, after the Web Module is deployed, use the test case test. Such efficiency is very low, because compiled, packaged, deployed, and testing time.

Spring's thoughts are testing early to find problems.

In the example, the test program was written for test SpringAppController:

Package tests;

Import java.util.map;

Import java.util.list;

Import java.io.ioException;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import javax.servlet.servletException;

Import junit.framework.testcase;

Import org.springframework.context.ApplicationContext;

Import org.springframework.context.support.filesystemxmlapplicationContext;

Import org.springframework.Web.Servlet.ModelandView;

Import Web.SpringAppController;

Import Bus.ProductManager;

Import bus.product;

Public Class TestspringAppController Extends Testcase {

Private applicationContext ac;

Public void setup () THROWS IOException {

AC = New FileSystemXmlapplicationContext ("SRC / TESTS / Web-INF / SPRINGAPP-Servlet.xml");

}

Public void testHandleRequest () throws servletexception, ioException {

SpringAppController sc = (SpringAppController) ac.getbean ("SpringAppController");

ModelandView MAV = Sc.HandleRequest NULL, (httpservletResponse) NULL);

Map m = mav.getmodel ();

List pl = (list) ((Map) M.Get ("model")). Get ("products");

Product P1 = (Product) PL.GET (0);

Assertequals ("lamp", p1.getdescription ());

PRODUCT P2 = (Product) Pl.get (1);

Assertequals ("Table", P2.GetDescription ());

PRODUCT P3 = (Product) Pl.get (2);

Assertequals ("chair", p3.getdescription ());

}

The test class inherits from junit.framework.testcase. When testing, it is mainly to test the model pair of Controler generated. Of course, in the above example, there is not much significance of test because Model is too simple. But if the logic generated by MODEL is very complicated, this test is very meaningful.

Note that in the above example

Public void setup () THROWS IOException {

AC = New FileSystemXmlapplicationContext ("SRC / TESTS / Web-INF / SPRINGAPP-Servlet.xml");

}

It creates a environment similar to the App Server for Controler (BEAN's loading is completed here)

转载请注明原文地址:https://www.9cbs.com/read-110497.html

New Post(0)