Environment version
Windows 8.1
Ide:instellij Idea 13
Spring:spring 4.1.1 & Spring MVC 4.1.1
WebLogic 10.3.0
jdk:1.6
In the previous article, the Web application we created was actually a spring MVC-based Web project.
Because we chose Spring 4.1.1 and Spring MVC 4.1.1,idea to help us automatically download the relevant jar packages when we created them, we just need to do some configuration.
(1) Open Web. Xml to edit content:
<Web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <Servlet-name>Mvc-dispatcher</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Mvc-dispatcher</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping> <!--log4j -- <Context-param> <Param-name>Log4jconfiglocation</Param-name> <Param-value>/web-inf/log4j.properties</Param-value> </Context-param> <Listener> <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class> </Listener></Web-app>
It is important to note that if you specify the name Mvc-dispatcher, you must create a file that begins with Mvc-dispatcher to configure Spring MVC.
(2) Open mvc-dispatcher-servlet.xml, edit content:
<Beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-conte Xt.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "
> <!--Scan all controllers-- <Context:component-scan Base-package= "Com.hellomvc.controllers"/> <!--start Note driver Springmvc function -- <Mvc:annotation-driven />
<!--handle static resources -- <Mvc:default-servlet-handler /> <Bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Property name= "prefix" value= "/pages/"/> < Property name= "suffix" value= ". jsp"/> </Bean> <!--tiles configuration -- <Bean ID= "Viewresolver" class= "Org.springframework.web.servlet.view.UrlBasedViewResolver"> < Property name= "Viewclass"> <value>Org.springframework.web.servlet.view.tiles2.TilesView</value> </ Property> < Property name= "Order" value= "1" /> </Bean> <Bean ID= "Tilesconfigurer" class= "Org.springframework.web.servlet.view.tiles2.TilesConfigurer"> < Property name= "Definitions"> <List> <value>/web-inf/tiles.xml</value> </List> </ Property> </Bean></Beans>
It is important to note that because we use the tiles framework in the project, it is not used without adding.
In addition,<mvc:default-servlet-handler/> because in Web. XML, we configure the Dispatcherservlet request map to "/", so Spring MVC captures all requests from the Web container, including requests for static resources, and Spring MVC treats them as a normal request, so finding a corresponding processor will result in an error. With <mvc:default-servlet-handler/>, If a request is found to be a static resource, the request is forwarded to the Web application server by default servlet processing, if it is not a request for a static resource, To continue processing by Dispatcherservlet.
(3) We use the tiles framework in our project, and we'll look at the configuration of Tiles.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE tiles-definitions Public "-//apache software foundation//dtd Tiles Configuration 2.0//en" "Http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name= "Base.definition" Template= "/pages/shared/layout.jsp"> <Put-attribute name= "Body" value="" /> </definition> <definition name= "home.*" extends= "Base.definition"> <Put-attribute name= "Body" value= "/pages/{1}.jsp"/> </definition> </tiles-definitions>
Here we define an underlying template "Base.definition", corresponding to the template file:/pages/shared/layout.jsp, which defines a "body" attribute. defines a wildcard template name for "Home.*", which inherits from "Base.definition", where the Body property, corresponding template file:/pages/{1}.jsp, where {1}, is obtained according to the wildcard character.
(4) Look at the controller class file again
This returns "Home.index", which is pointed to the Tiles.xml configuration view file.
(5) Finally, look at the Complete directory structure:
(6) Operation
Create a spring MVC application using Instellij idea