Spring + Hibernate + struts Tiles Layout Framework

Source: Internet
Author: User

Some Web applications use struts's Tiles Layout Framework to open design pages. I don't think the pages are simpler. There are too many configuration files and template pages, which is confusing, but I should record them, in case you use it one day...

The web. xml of the program is written in this way.

<? Xml version = "1.0" encoding = "UTF-8"?>
<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> dispatcher </servlet-name>
<Servlet-class>
Org. springframework. web. servlet. DispatcherServlet
</Servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>
WEB-INF/config/dao-config.xml, WEB-INF/config/view-config.xml, WEB-INF/config/controller-config.xml, WEB-INF/config/managerController-config.xml
</Param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> dispatcher </servlet-name>
<Url-pattern> *. lzw </url-pattern>
</Servlet-mapping>
</Web-app>

For example, in the red part, when tomcat loads the program, the program reads the configuration files according to the path,

The view-config.xml is a file that configures the Mappings of url requests on the page, as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "urlHandlerMapping"
Class = "org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">
<Property name = "mappings">
<Props>
<! -- Foreground -->
<Prop key = "/index. lzw"> goodsController </prop>
<Prop key = "/goods *. lzw"> goodsController </prop>
<Prop key = "/register. lzw"> registerController </prop>
<Prop key = "/login. lzw"> loginController </prop>
<Prop key = "/modifyMember. lzw"> modifyMemberController </prop>
<Prop key = "/order. lzw"> orderController </prop>
<Prop key = "/sellSort. lzw"> sellSortController </prop>
<Prop key = "/placardDetails. lzw"> placardDetailsController </prop>
<Prop key = "/goodsDetails. lzw"> goodsDetailsController </prop>
<Prop key = "/type. lzw"> typeController </prop>
<Prop key = "/cart *. lzw"> cartController </prop>
<Prop key = "/cartSee. lzw"> cartSeeController </prop>
<Prop key = "/cartCheckout. lzw"> cartCheckoutController </prop>
<Prop key = "/orderDetails. lzw"> orderDetailsController </prop>
<Prop key = "/searchController. lzw"> searchController </prop>
<! -- Background -->
<Prop key = "/loginM. lzw"> loginMController </prop>
<Prop key = "/indexM. lzw"> indexMController </prop>
<Prop key = "/superType. lzw"> superTypeController </prop>
<Prop key = "/superAdd. lzw"> superAddController </prop>
<Prop key = "/subType. lzw"> subTypeController </prop>
<Prop key = "/subTypeAdd. lzw"> subTypeAddController </prop>
<Prop key = "/goodsAdd. lzw"> goodsAddController </prop>
<Prop key = "/goodsDetailM. lzw"> goodsDetailsMController </prop>
<Prop key = "/goodsModify. lzw"> goodsModifyController </prop>
<Prop key = "/goodsDel. lzw"> goodsDelController </prop>
<Prop key = "/member *. lzw"> memManagerController </prop>
<Prop key = "/orderM *. lzw"> orderManagerController </prop>
<Prop key = "/placardManage. lzw"> placardManageController </prop>
<Prop key = "/placardAdd. lzw"> placardAddController </prop>
</Props>
</Property>
</Bean> <! --
<Bean id = "exceptionResolver"
Class = "org. springframework. web. servlet. handler. SimpleMappingExceptionResolver">
<Property name = "exceptionMappings">
<Props>
<Prop key = "java. lang. Exception"> Error </prop>
</Props>
</Property>
</Bean>
--> <Bean id = "tilesConfiger"
Class = "org. springframework. web. servlet. view. tiles. TilesConfigurer">
<Property name = "definitions">
<List>
<Value> WEB-INF/config/tiles-template.xml </value>
<Value> WEB-INF/config/tiles-defs.xml </value>
<Value> WEB-INF/config/tiles-managerDefs.xml </value>
<Value> WEB-INF/config/tiles-manageTemplate.xml </value>
</List>
</Property>
</Bean>
<Bean id = "viewResolver"
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass">
<Value>
Org. springframework. web. servlet. view. tiles. TilesJstlView
</Value>
</Property>
</Bean>
</Beans>

When I open the homepage, a request/index will be sent to the homepage. lzw, web. all *. lzw requests are all managed by spring, so no struts controller is required ??!), So the request reaches the view-config.xml, according to the <prop key = "/index. lzw "> goodsController </prop> we need to find the Controller's java class in the controller-config.xml file, the controller-config.xml file is as follows:
 

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "searchController" class = "com. lzw. SearchController">
<Property name = "dao">
<Ref bean = "daoProxyFactory"/>
</Property>
</Bean>
<! -- Product Controller -->
<Bean id = "goodsMethodResolver"
Class = "org. springframework. web. servlet. mvc. multiaction. PropertiesMethodNameResolver">
<Property name = "mappings">
<Props>
<Prop key = "/index. lzw"> goodsShow </prop>
<Prop key = "/goodsNewGoods. lzw"> newGoods </prop>
<Prop key = "/goodsSale. lzw"> saleGoods </prop>
</Props>
</Property>
</Bean>
<Bean id = "goodsController" class = "com. lzw. NewGoodsController">
<Property name = "methodNameResolver">
<Ref local = "goodsMethodResolver"/>
</Property>
<Property name = "dao">
<Ref bean = "daoProxyFactory"/>
</Property>
<Property name = "saleGoodsLine">
<Value> 1 </value>
</Property>
<Property name = "newGoodsLine">
<Value> 3 </value>
</Property>
</Bean>

.

.

.

</Beans>

So the program will know to enter com. lzw. the NewGoodsController action is run, such as the blue font display. The configuration file assigns values to the instance variables in the action, for example, assigning values to saleGoodsLine and newGoodsLine to 1 and 3, and injecting dependencies to the dao attribute, daoProxyFactory can be obtained from the dao-config.xml for hibernate operations in the action. <Property name = "methodNameResolver"> the goodsMethodResolver bean is referenced from the current file. For example, if the bean is displayed in a green font, the function of this bean is to use/index. the lzw url matches a goodsShow method, that is, to execute the goodsShow method in the action, this method is as follows:

Public class NewGoodsController extends MultiActionController {
Private Dao;
Private int saleGoodsLine = 1;
Private int newGoodsLine = 4;
Public ModelAndView goodsShow (HttpServletRequest request, HttpServletResponse response ){
List list1 = dao. getShowSaleGoodsList ();
List list2 = dao. getShowNewGoodsList ();
Map model = new HashMap ();
Model. put ("saleGoods", list1 );
Model. put ("newGoods", list2 );
Model. put ("saleGoodsLine", saleGoodsLine );
Model. put ("newGoodsLine", newGoodsLine );
Return new ModelAndView ("index", model );
}

}

Action will show the queried information in a Map and pass it to the page. How does spring determine which page to pass? Four files are introduced in the view-config.xml file:

<Bean id = "tilesConfiger"
Class = "org. springframework. web. servlet. view. tiles. TilesConfigurer">
<Property name = "definitions">
<List>
<Value> WEB-INF/config/tiles-template.xml </value>
<Value> WEB-INF/config/tiles-defs.xml </value>
<Value> WEB-INF/config/tiles-managerDefs.xml </value>
<Value> WEB-INF/config/tiles-manageTemplate.xml </value>
</List>
</Property>
</Bean>

These are the configuration files of the tiles component, as shown in the red above, action passes a Map key for index, you can find the matching ing in the tiles-defs.xml:

<Tiles-definitions>
<! -- Homepage -->
<Definition name = "index" extends = ". indexLayout"/>

</Tiles-definitions>

The index guide goes to the. indexLayout definition, which is in the tiles-template.xml:

<Tiles-definitions>
<! -- Error page template -->
<Definition name = "Error" path = "/WEB-INF/jsp/template/error. jsp"/>
<! -- Homepage template -->
<Definition name = ". indexLayout"
Path = "/WEB-INF/jsp/template/indexTemplate. jsp">
<Put name = "title" value = "Xin Xiang electronic mall"/> <! -- Website title -->
<Put name = "navigation" value = "/WEB-INF/jsp/navigation. jsp"/> <! -- Navigation bar -->
<Put name = "left" value = ". left"/> <! -- Left menu -->
<Put name = "search" value = ". search"/> <! -- Search bar -->
<Put name = "content" value = "/WEB-INF/jsp/productInfo. jsp"/> <! -- Content column -->
<Put name = "right" value = ". right"/> <! -- Right menu -->
<Put name = "footer" value = "/WEB-INF/jsp/footer. jsp"/> <! -- Copyright information -->
</Definition>

The above is mainly about a basic process of using spring controller with tiles. Below we will record how struts's Tiles layout framework works. For example, on the homepage, create a template page:

<% @ Page contentType = "text/html; charset = gb2312" language = "java"
ErrorPage = "error. jsp" %>
<% @ Taglib prefix = "tiles"
Uri = "http://jakarta.apache.org/struts/tags-tiles" %>
<Html>
<Head>
<Title> <tiles: getAsString name = "title"/>
</Title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Link href = "CSS/style.css" rel = "stylesheet">
</Head>
<Body>
<Center>
<Table align = "center" width = "790" border = "0" cellspacing = "0"
Cellpadding = "0">
<Tr>
<Td class = "tableBorder">
<Tiles: insert attribute = "navigation"/> </td>
</Tr>
<Tr>
<Td class = "tableBorder" bgcolor = "white">
<Tiles: insert attribute = "search"/>
<Table width = "100%" height = "500" border = "0" cellpadding = "0"
Cellspacing = "0">
<Tr>
<Td valign = "top" width = "198" align = "right" bgcolor = "# F0F0F0">
<Tiles: insert attribute = "left"/> </td>
<Td valign = "top">
<Table width = "100%" cellpadding = "0" cellspacing = "0">
<Tr>
<Td valign = "top" width = "69%" bgcolor = "white">
<Tiles: insert attribute = "content"/> </td>
& Lt; td valign = "top" width = "31%" & gt;
<Tiles: insert attribute = "right"/> </td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
<Table width = "790" border = "0" align = "center" cellpadding = "0"
Cellspacing = "0">
<Tr> <td> <tiles: insert attribute = "footer"/> </td> </tr>
</Table>
</Td>
</Tr>
</Table>
</Center>
</Body>
</Html>

We can see that more labels such as <tiles: insert attribute = "navigation"/> </td> are used. attribute values must be the same
The <put name = "navigation" value = "/tiles-template.xml/jsp/navigation. jsp"/> name values in the WEB-INF are consistent. In this way, the page of navigation. jsp is inserted. This article describes how to use spring as a controller. If struts is used as a controller, the configuration files of tiles must specify the path in the struts configuration file:

<Plug-in className = "org. apache. struts. tiles. TilesPlugin">
<Set-property = "definitions-config" value = "/WEB-INF/tiles-defs.xml"/>
<Set-property = "definitions-parser-validate" value = "true"/>
</Plug-in>

· Definitions-config parameter: Specifies the configuration file of the Tiles component. If multiple configuration files exist, they are separated by commas.
· Definitions-parser-validate parameter: Specifies whether the XML parser verifies the Tiles configuration file. Optional values include true and false. The default value is true.
To ensure that the TilesPlugin plug-in is loaded when the Web application starts, the ActionServlet controller should be added. The ActionServlet controller can load all the plug-ins during initialization. The following code configures ActionServlet in the web. xml file:

<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Init-param>
<Param-name> config </param-name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Load-on-startup> 3 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>


 

</Tiles-definitions>

Based on this definition, the program will build the page. Let's take a look at a part of the productInfo. jsp page:

<Table width = "100%" height = "200" border = "0" cellpadding = "0"
Cellspacing = "0">
<C: forEach begin = "1" end = "$ {saleGoodsLine * 2}" var = "line" step = "2">
<Tr>
<C: forEach begin = "$ {line-1}" end = "$ {line}" var = "goods" items = "$ {saleGoods}">
<Td width = "49%" height = "200" valign = "top">
<Table width = "100%" border = "0" cellpadding = "0"
Cellspacing = "0">
<Tr>
<Td height = "95" align = "center">
Height = "75"> </td>
</Tr>
<Tr>
<Td height = "22" align = "center">
<A href = "goodsDetails. lzw? Id =$ {goods. id} ">
$ {Goods. goodsName} </a> </td>
</Tr>
<Tr>
<Td height = "20" align = "center"
Style = "text-decoration: line-through; color: # FF0000">
Original price: $ {goods. price} </td>
</Tr>
<Tr>
<Td height = "20" align = "center">
Current Price: $ {goods. nowPrice} </td>
</Tr>

</Table>
</Td>
</C: forEach>
</Tr>
</C: forEach>
</Table>

The page uses the el expression to obtain the values in the Map.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.