Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the page layout in NetBeans Struts?

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how the page layout in NetBeans Struts is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In the previous development process, I often encountered the experience that in order to accomplish a task, the page designer first designed the page prototype and then handed it over to the programmer. In many cases, we always have to organize a complete page. In fact, we have done too much repetitive work. The structure of many pages is exactly the same, the head, tail and navigation menu of the page are all the same, but the main content is different. Tidying up these same parts over and over again is a big burden, and it's natural to think that if these page elements can be reused, it will save a lot of work.

Struts has built-in Tiles, which makes it easy to reuse pages, just like we reuse classes.

Use Tiles

In the previous development process, I often encountered the experience that in order to accomplish a task, the page designer first designed the page prototype and then handed it over to the programmer. In many cases, we always have to organize a complete page. In fact, we have done too much repetitive work. The structure of many pages is exactly the same, the head, tail and navigation menu of the page are all the same, but the main content is different. Tidying up these same parts over and over again is a big burden, and it's natural to think that if these page elements can be reused, it will save a lot of work.

Struts has built-in Tiles, which makes it easy to reuse pages, just like we reuse classes.

Use Tiles

1. If the page content is divided into pages, a page can be divided into many blocks, such as header, footer, menu, content, etc., if the news portal, content can be further subdivided, footall, entertainment, and so on.

Here is just a demonstration, I will divide the page into header, footer, content.

Expand Configuration Files, open tile-defs.xml, define a most basic page template, and expand other pages based on this (this is a bit like defining a base class, and the other subclasses inherit the base class to get the properties of the base class).

< definition name= ".mainLayout" path= "/ layouts/default.jsp" > < put name= "title" value= "Struts Demo Application" / > < put name= "header" value= "/ layouts/header.jsp" / > < put name= "footer" value= "/ layouts/footer.jsp" / > < put name= "body" value= ".portal.body" / > < / definition >

Other pages are based on this, inherit the template content, and can redefine title and body content.

< definition name= "logonPage" extends= ".mainLayout" > < put name= "title" value= "Logon Page" / > < put name= "body" value= "/ layouts/logonBody.jsp" / > < / definition > < definition name= "registerPage" extends= ".mainL ayout" > < put name= "title" value= "New Registeration" / > < put name= "body" value= "/ layouts/registerBody.jsp" / > < definition name= "indexPage" extends= ".mainLayout" > < put name= "title" value= "Home Page" / > < put name= "body" value= "/ layouts/indexBody.jsp" / > < / definition > < definition name= "registeredSuccessPage" extends= ".mainLayout" > < put name= "title" value= "Registered Successfully" / > < put name= "body" value= "/ layouts/registerSuccessBody.jsp" / > < / definition >

In fact, this is very similar to class inheritance in Java.

two。 For the corresponding jsp page, create a new layouts directory under Web pages. The default template (that is, default.jsp) is as follows.

< html > < head > < meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" < title > < tiles:getAsString name= "title" / > < / title > < / head > < body > < div id= "header" > < tiles:insert attribute= "header" / > < / div > < div id= "content" > < tiles:insert attribute= "body" / > < / div > < div id= "footer" > < tiles:insert attribute= "footer" / > < / div > < / body > < / html >

Where getAsString means that title inserts the template in the form of a string.

The page title is defined in header.jsp.

<% @ page contentType= "text/html"% > <% @ page pageEncoding= "UTF-8"% > < div > < H2 > Struts Demo Application < / H2 > < / div >

The footer.jsp content version statement is as follows:

<% @ page contentType= "text/html"% > <% @ page pageEncoding= "UTF-8"% > < div > Copyright 2007 ©hantsy < / div >

Copy one copy of each of the other pages to the layouts directory, leaving only the content between body.

For example, the indexBody.jsp content is as follows:

< @ page contentType= "text/html" >

< @ page pageEncoding= "UTF-8" >

< @ taglib uri= "/ WEB-INF/struts-html.tld" prefix= "html" >

< @ taglib uri= "/ WEB-INF/struts-bean.tld" prefix= "bean" >

< @ taglib uri= "/ WEB-INF/struts-logic.tld" prefix= "logic" >

< H2 > Home Page < / H2 >

Welcome

< logic:notPresent name= "username" scope= "session" >

Guest! < html:link forward= "newLogon" > Logon < / html:link > or

< html:link forward= "newRegister" > Register Now... < / html:link >

< / logic:notPresent >

< logic:present name= "username" scope= "session" >

< bean:write name= "username" scope= "session" / >

< html:link forward= "logout" > Log Out.. < / html:link >

< / logic:present >

3. Modify the Struts configuration file to replace all jsp file paths with the names defined in the tile configuration file.

< action-mappings > < action path= "/ logout" type= "com.myapp.web.LogoutAction" / > < action input= "registerPage" name= "UserForm" path= "/ register" scope= "session" type= "com.myapp.web.RegisterAction" > < forward name= "success" path= "registeredSuccessPage" / > < action input= "logonPage" name= "UserForm" path= "/ logon" scope= "session" type= "com.myapp.web.LogonAction" / > < action path= "/ index" forward= "indexPage" / > < action path= "/ newLogon newLogon" "logonPage" / > < action path= "/ newRegister" forward= "registerPage" / > < / action-mappings >

It is worth noting that NetBeans has helped us automate the two most important configurations necessary to use Tile when creating the project.

One is to register the Tile plug-in, and we can see it by opening the Struts configuration file struts-config.xml.

< plug-in className= "org.apache.struts.tiles.TilesPlugin" > < set-property property= "definitions-config" value= "/ WEB-INF/tiles-defs.xml" / > < set-property property= "moduleAware" value= "true" / > < / plug-in >

The other is to redefine controller and change processorClass to use org.apache.struts.tiles.TilesRequestProcessor.

< controller processorClass= "org.apache.struts.tiles.TilesRequestProcessor" / >

4. Run the project for testing.

Use Sitemesh

Sitemesh is another excellent page layout tool brought by www.opensymphony.org. It is not specific to Struts, and even web programs in other languages can use it.

It uses the Decorator mode to achieve the desired results. Here, pages can be divided into two categories, decorator (retouched) and decoratored (decorated). It's like having a picture frame and a variety of different photos that can be used to transform. When you put different photos into the frame, you get different visual effects.

1. Download and install Sitemesh.

Download the latest sitemesh 2.3 from www.opensymphony.org and extract it to your hard drive.

Add common- collections.jar,freemarker.jar, velocity-deps-1.3.1.jar,velocity-tools-view-1.3.1.jar under < sitemesh > / lib and sitemesh-2.3.jar under < sitemesh > / dist to the project Libraries.

Copy a copy of the sitemesh-decorator.tld and sitemesh-page.tld of < sitemesh >\ src\ etc\ tld\ jsp1.2 to the web/WEB-INF/ of the project.

two。 Configure sitemesh.

Open web.xml from configurations and define Sitemesh Filter.

< filter > < filter-name > sitemesh < / filter-name > < filter-class > com.opensymphony.module.sitemesh.filter.PageFilter < / filter-class > < / filter > < filter-mapping > < filter-name > sitemesh < / filter-name > < url-pattern > / * < / url-pattern > < / filter-mapping > two basic sitemesh configuration files are defined in web/WEB-INF, which can be copied by sitemesh package.

Sitemesh.xml defines the most basic application rules.

< sitemesh > < property name= "decorators-file" value= "/ WEB-INF/decorators.xml" / > < excludes file= "${decorators-file}" / > < page-parsers > < parser content-type= "text/html" class= "com.opensymphony.module.sitemesh.parser.HTMLPageParser" / > < / page-parsers > < decorator-mappers > < mapper class= "com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper" > < param name= "property.1" value= "meta.decorator" / > < param name= "property.2" value= Decorator "/ > < mapper class=" com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper "> < / mapper > < mapper class=" com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper "> < param name=" match.MSIE "value=" ie "/ > < param name=" match.Mozilla ["value=" ns "/ > < param name=" match.Opera "value=" opera "/ > < param name=" match.Lynx "value=" lynx "/ > < / mapper > < mapper class=" com.opensymphony.module.sitemesh " < param name= decorator "value=" printable "/ > < param name=" parameter.name "value=" printable "/ > < param name=" parameter.value "value=" true "/ > < / mapper > < mapper class=" com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper > < param name= "decorator" value= "robot" / > < mapper > < mapper class= "com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper" > < param name= "decorator.parameter" value= "decorator" / > < param name= < parameter.name "value=" confirm "/ > < param name=" parameter.value "value=" true "/ > < / mapper > < mapper class=" com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper "> < / mapper > < mapper class=" com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper "> < param name=" config "value=" ${decorators-file} "/ > < / mapper > < / decorator-mappers > < / sitemesh >

Specific decortor application rules are defined in decorators.xml.

< decorators defaultdir= "/ decorators" >

< decorator:head / > < / head > < body > < div > < table width=" 100% "> < tr > < td id=" pageTitle "> < H2 > < decorator:title / > < / H2 > < / td > < / tr > < tr > < td valign=" top "height=" 100% "> < decorator:body / > td > < / tr > < tr > < td id=" footer "> < page:applyDecorator page=" / footer.html "name=" panel " / > < / td > < / tr > < / table > < / div > < / body > < / html >

Define another panel.

<% @ taglib uri= "http://www.opensymphony.com/sitemesh/decorator" prefix=" decorator "% >

< p >

< table width= "100%" border=0 cellpadding=0 cellspacing=0 >

< tr >

< td class= "panelBody" >

< decorator:body / >

< / td >

< / tr >

< / table >

< / p >

In the file, decorator:title and decorator:body define the location of the content to be replaced, which, after application, is replaced by the content between the title and body tags of the decoratored page, respectively.

Let's restore the page under Web Pages to its original appearance. For example, the index.jsp content is as follows.

< @ page contentType= "text/html" >

< @ page pageEncoding= "UTF-8" >

< @ taglib uri= "/ WEB-INF/struts-html.tld" prefix= "html" >

< @ taglib uri= "/ WEB-INF/struts-bean.tld" prefix= "bean" >

< @ taglib uri= "/ WEB-INF/struts-logic.tld" prefix= "logic" >

< html >

< head > < title > HomePage < / title > < / head >

< body >

Welcome

< logic:notPresent name= "username" scope= "session" >

Guest! < html:link forward= "newLogon" > Logon < / html:link > or

< html:link forward= "newRegister" > Register Now... < / html:link >

< / logic:notPresent >

< logic:present name= "username" scope= "session" >

< bean:write name= "username" scope= "session" / >

< html:link forward= "logout" > Log Out.. < / html:link >

< / logic:present >

< / body >

< / html >

At the same time, modify the Struts definition file to make all the page names defined by tiles in the previous section into a specific page path.

< action-mappings > < action path= "/ logout" type= "com.myapp.web.LogoutAction" / > < action input= "/ register.jsp" name= "UserForm" path= "/ register" scope= "session" type= "com.myapp.web.RegisterAction" > < forward name= "success" path= "/ registerSuccess.jsp" / > < action input= "/ logon.jsp" name= "UserForm" path= "/ logon" scope= "session" type= "com.myapp.web.LogonAction" / > < action path= "/ Welcome" forward= "/ welcomeStruts. Jsp "/ > < action path=" / index "forward=" / index.jsp "/ > < action path=" / newLogon "forward=" / logon.jsp "/ > < action path=" / newRegister "forward=" / register.jsp "/ > < / action-mappings >

Also disable tiles to avoid conflicts with sitemesh. Comment out the controller definition and let Struts use the default requestProcessor.

About how the page layout in NetBeans Struts is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report