Spring Getting Started 18 - DispatcherServlet Definition

xiaoxiao2021-03-06  36

Getting Started 18 - DispatcherServlet Definition

DispatcherServlet presets using the servlet name plus "-Servlet.xml" as its defined file name, defining gear presets Under Web-INF, you can specify the definition file name and location, for example:

Hello

Org.springframework.web.servlet.dispatcherServlet

contextconfiglocation

/Web-inf/hello1-servlet.xml, /web-inf/hello2-servlet.xml

DispatcherServlets have their Namespace and application Context, which you can define multiple DispatcherServlets in Web.xml, which are responsible for request processing of different modules. Spring is handled by the components handled by the DispatCherServlet as a general bean, so when setting the DispatCherServlet, just like the Bean definition file in the top theme, the consistent configuration is the Spring Office One of the advantages provided, each component is set like a bean, so you can use these components as long as you query the API file, understand the relationship between properties, and set the dependencies in the defined file. Some setup files for setting DispatcherServlets, and we have introduced most of our previous topics, which add some additional settings. First, we have previously set the Hellouseraction, when setting the ViewPage, you must set a complete directory and file name, for example:

Hello!

/web-inf/jsp/hellouser.jsp

You can set the Prefix and Suffix properties in ViewResolver, set the front and back text except for the file name, so your ViewPage property can only file the main name of the file, give an instance directly:

Org.springframework.web.servlet.view.internalResourceView

/ web-inf / jsp /

.jsp

Hello!

Hellouser

You can also independent of ViewResolver's definition to a property file, using org.springframework.web.servlet.view.resourceBundLeViewResolver, the benefit of this is that you can set different representation layer technology based on your actual URL, for example :

views

Hello!

Hellouser

We set a Views name in ResourceBundLeviewResolver's baseName (actually, views is also the default value of BaseName), indicating that we will use Views.properties to set the viewclass with the URL of the layer technology, our views.properties as follows:

Hellouser.class = org.springframework.meb.servlet.view.internalResourceView

Hellouser.url = / Web-inf / jsp / hellouser.jsp

WelcomeUser.class = org.springframework.web.servlet.view.jstlview

WelcomeUser.url = / web-inf / jsp / welcomeUser.jsp

OtherView.class = org.springframework.web.servlet.view.velocityVIEW

OtherView.url = / web-inf / vm / other.vm

With this way, we can mix different representation layer technologies in the same web application, and you are the Class and URL value settings in Views.properties. The name of Hellouser and WelcomeUser is the name of our ModelandView, for example we set. Hellouser gives ModelandView, you will go to Hellouser.Class and hellouser.URL to set the representation layer technology with the URL, and we are injecting this by hellouseraction viewPage property, avoiding the name in the program. If your JSP page or servlet will drop out, you want to capture this exception, in addition to the container and JSP web page setting method (refer to the introduction in the JSP / Servlet layout), you can also define files in DispatcherServlet Set the error handling web page, set an ExceptionResolver bean, such as SimpleMappingexceptionResolver: ....

Org.springframework.web.servlet.view.internalResourceView

/ web-inf / jsp /

.jsp

EXIO

....

So set, as long as SQLException occurs, it will be connected to /Web-inf/jsp/EXSQL.JSP, and IOEXception occurs, and it will be connected to /Web-inf/jsp/EXIO.JSP.

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

New Post(0)