The Spring,hibernate framework does, yes, and jquery. I use jquery to do asynchronous request to the background, the generation of JSON data back to the foreground generation drop-down input box, request to the background, the successful generation of JSON data and based on the struts mapping file back to a JSP page, the role of this page is only to generate text data, but JS has not responded ... From the Internet to find Jquery.ajax has a parameter error, it can tell me what's wrong with this, error code Plus, alert, there is an error: ParserError
On the Internet, there are probably several reasons for this error:
1. The JSON string returned in the background is not in the correct format
2, configuration file, or the problem caused by hand errors in the code
Careful troubleshooting, the first string format problem does not exist, and then carefully look at the comparison of the previous code, or no problem, really let people despair ...
Finally decided to look at all the infrequently used configuration files in the project, found the title file, found that the original problem in this, its role is to simplify the JSP code duplication of work (introduction of common pages, JS, CSS and so on. is equivalent to include, which is more concise than the include in JSP). But it has all the pages are added template, just I this JSP does not need a template, to this, the problem found, put my definition of the action in its excludes list. Problem solving.
Brief introduction:
Sitemesh applies decorator mode, intercepts request and response with filter, and combines the page component Head,content,banner into a full view. Usually we are using the include tag in each JSP page to constantly contain a variety of headers, stylesheet, scripts and footer, now, with the help of Sitemesh, we can happily delete them. For example, you want to easily reach the composite view mode, then read this article.
First, copy the following filter definitions in Web-inf/web.xml:
<?xml version= "1.0" encoding= "GBK"?>
<web-app 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"
version= "2.4" >
<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>
</web-app>
Second, copy of the required Sitemesh-2.3.jar to Web-inf\lib. (Here you can download http://www.opensymphony.com/sitemesh/) Iii. build web-inf/decorators.xml describe each adorner page.
<decorators defaultdir= "/decorators" >
<decorator name= "main" page= "main.jsp" >
<pattern>*</pattern>
</decorator>
</decorators>
The above configuration file specifies the path to the adorner page and specifies a adorner named main that decorates all pages under the Web App root path by default.
Iv. setting up the adorner page/decorators/main.jsp
#
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ taglib uri= "Http://www.opensymphony.com/sitemesh/decorator" prefix= "decorator"%> <title><decorator:title default= "Adorner page ..."/></title>
<decorator:head/>
<body>
Examples of Sitemesh <decorator:body/>
</body>
V. To create a decorated page/index.jsp (content page)
#
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<title>agent test</title>
<body>
<p> This page only one sentence, is this sentence .</p>
</body>
Last access to index.jsp will generate the following page:
Also, all the pages will be like index.jsp, modified by the Sitemesh filter using the trim mode, without using the include tag.
Decorator Decorator Concept
A common approach to building reusable Web applications is to build a tiered system, like the following common Web application:
* Front end: JSP and Servlets, or Jakarta velocity ...
* Control Layer Framework Controller: (Struts/webwork)
* Business logic: Key business logic
* Persistence framework: HIBERNATE/JDO
But the bad thing is that the front page logic is hard to reuse, and when you use countless include in each page to repeat the public header, stylesheet, Scripts,footer, a problem arises--duplicate code, Each page must be copied over the page structure, and when you need to creatively change the structure of the page, the disaster falls in love with you.
Sitemesh intercepts the request and response through the filter and adds a certain decoration (possibly Header,footer ...) to the original page, then returns the result to the client, and the original page that was decorated does not know the Sitemesh decoration, This also achieves the purpose of decoupling.
It is said that the upcoming Portlet specification will help us to achieve the standard of implementation more than these more cool ideas, but poor I still do not understand it is a what is the end, interested people can study
Jetspeed, or JSR (Java specification Request) 168, but I think Sitemesh is so simple, we might as well use it first.
Let's see how to configure the environment
In addition to copy to Web-inf/lib in the Sitemesh.jar, there are 2 files to be established to web-inf/:
* Sitemesh.xml (optional)
* Decorators.xml
Sitemesh.xml can set up 2 kinds of information:
Page parsers: is responsible for reading the data of the stream into a Page object to be parsed and manipulated by Sitemesh. (less common, default)
Decorator mappers: Different types of adorners, I found 2 kinds of comparative useful are listed below. A common mapper that can specify the adorner's profile name, and another printable adorner that allows you to give the original page for printing when accessed in Http://localhost/aaa/a.html?printable=true mode ( Lest the fancy pictures of the header, footer, etc.)
(but it is generally not necessary to establish it, the default setting is sufficient: Com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):
Example:
<sitemesh>
<page-parsers>
<parser default= "true" class= "Com.opensymphony.module.sitemesh.parser.DefaultPageParser"/>
<parser content-type= "text/html;charset=iso-8859-1" class= " Com.opensymphony.module.sitemesh.parser.FastPageParser "/>
</page-parsers>
<decorator-mappers>
<mapper class= "Com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper" >
<param name= "config" value= "/web-inf/decorators.xml"/>
</mapper>
<mapper class= "Com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper" >
<param name= "decorator" value= "printable"/>
<param name= "parameter.name" value= "Printable"/>
<param name= "Parameter.value" value= "true"/>
</mapper>
</decorator-mappers>
</sitemesh>
Decorators.xml: Defines the description of all page artifacts that make up the composite view (main structure page, Header,footer ...), as in the following example:
<decorators defaultdir= "/decorators" >
<decorator name= "main" page= "main.jsp" >
<pattern>*</pattern>
</decorator>
<decorator name= "printable" page= "printable.jsp" role= "Customer" webapp= "AAA"/>
</decorators>
* DefaultDir: The directory containing the adorner page
* Page: File name
* Name: Alias
* Role: Roles for security
* WebApp: You can also specify this file to store the directory
* Patterns: Matches the path that can be used *, those pages that are visited need to be decorated.
The most important thing is to write the adorner itself (that is, the pages to be reused, and the structure page).
In fact, the important job is to make the adorner page itself (that is, the page that contains the structure and the rules), and then describe them to the decorators.xml.
Let's take a look at the simplest usage: actually the most commonly used and simplest usage is our Hello example, in the face of so many technologies, I think as long as the functional donuts can be achieved, there is no need to study too deep (unless you have deeper needs).
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ taglib uri= "Http://www.opensymphony.com/sitemesh/decorator" prefix= "decorator"%>
<title><decorator:title default= "Adorner page ..."/></title>
<decorator:head/>
<body>
Examples of Sitemesh <decorator:body/>
</body>
We used only 2 tabs on the adorner page:
<decorator:title default= "Adorner page ..."/>: Inserts the title content of the original page of the request into the <title></title> center.
<decorator:body/>: Inserts the entire contents of the body of the requested original page into the appropriate location.
We then add the following description to the Decorator.xml:
<decorator name= "main" page= "main.jsp" >
<pattern>*</pattern>
</decorator>
This way, all the requested pages are re-processed and re-presented in front of you in the main.jsp format.
Let's take a look at more usage. (Plagiarism Sitemesh documents)
The following are all the tags:
Decorator Tags Page tags
is used to create a decorator page. Used to access adorners from the original content page.
<decorator:head/>
<decorator:body/>
<decorator:title/>
<decorator:getproperty/>
<decorator:usepage/>
<page:applydecorator/>
<page:param
<decorator:head/>
Insert the contents of the head tag (excluding the head tag itself) from the original page (the wrapped page).
<decorator:body/>
Insert the contents of the body tag of the original page (the wrapped page).
<decorator:title [default= ...]/>
Insert the contents of the title tag of the original page (the wrapped page), and you can add a default value.
Cases:
/decorator/main.jsp (Adorner page): <title><decorator:title default= "but save Title-hello"/>-Additional title </title>
/aaa.jsp in (original page): <TITLE>AAA page </title>
Results of accessing/aaa.jsp: <TITLE>AAA page-Additional titles </title>
<decorator:getproperty property= "..." [default= "..."] [writeentireproperty= "..."] />
You can also add a default value by inserting the contents of the original label's property on the label at the Origin page (the wrapped page).
The examples in the Sitemesh documentation are well understood:
The decorator: <body bgcolor= "White" <decorator:getproperty property= "Body.onload" writeentireproperty= "true"/ >>
The undecorated page: <body onload= "Document.someform.somefield.focus (); >
The decorated page: <body bgcolor= "white" onload= "document.someform.somefield.focus ();" >
Note that writeentireproperty= "true" adds a space before inserting the content.
<decorator:usepage id= "..."/>
Like the <jsp:useBean> tag in a JSP page, you can use a page that is packaged as a Page object. (Lazy to use)
Example: Available <decorator:usepage id= "page"/>: <%=page.gettitle ()%> reached <decorator:title/> access results.
<page:applydecorator name= "..." [page= "..." title= "..."] >
<page:param name= "..." > ... </page:param>
<page:param name= "..." > ... </page:param>
</page:applyDecorator>
Applies the wrapper to the specified page, which is typically used to actively apply the wrapper in the wrapped page. This tag is a bit difficult to understand, let's look at an example:
Wrapper page/decorators/panel.jsp:<p><decorator:title/></p> ... <p><decorator:body/></ P>
And there are <decorator name= "Panel" page= "panel.jsp" in Decorators.xml/>
A public page that will be packaged by the panel:/public/date.jsp:
... <%=new java.util.Date ()%> ... <decorator:getproperty property= "Myemail"/>
Package page/page.jsp:
Application of <title>page </title>
.....
<page:applydecorator name= "Panel" page= "/_public/date.jsp" >
<page:param name= "Myemail" > [email protected] </page:param>
</page:applyDecorator>
What will be the end result? In addition to the/page.jsp will be wrapped on the default packaging page Header,footer outside, Page.jsp page is also embedded in the date.jsp page, and this date.jsp page will also be panel.jsp wrapped as a title plus body has 2 paragraphs of the page, the 1th paragraph is date.jsp title, the 2nd paragraph is date.jsp body content.
In addition, the attribute values declared by the Page:param tag contained in Page:applydecorator can also be accessed in the wrapper page with the decorator:getproperty tag.
Printable interface decoration
As mentioned earlier, there are 1 printable adorners that allow you to apply other adorners (as specified by yourself) when accessing with Http://localhost/aaa/a.html?printable=true, giving the original page for printing (lest the header be Footer such as the fancy pictures are also hooked).
Let's take a look at how to achieve him:
1. First set in Web-info/sitemesh.xml:
<mapper class= "Com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper" >
<param name= "decorator" value= "printable"/>
<param name= "parameter.name" value= "Printable"/>
<param name= "Parameter.value" value= "true"/>
</mapper>
This allows the printable=true to use the adorner named printable instead of the original adorner.
2. Define the corresponding printable adorner in the Web-info/decorators.xml
<decorator name= "printable" page= "printable.jsp"/>
3. Finally write the printable decorator/decorators/printable.jsp
<%@ taglib uri= "Sitemesh-decorator" prefix= "decorator"%>
<title><decorator:title/></title>
<decorator:head/>
<body>
<p align= "Right" ><i> (printable version) </i></p>
<decorator:body/>
</body>
This allows an original page to switch between different adorner pages via the printable=true switch.
Chinese questions
Since the default character set used internally by Sitemesh is iso-8859-1, the direct use will be garbled, and we can correct it by the following methods:
* Method 1: You can find in the configuration file of the application server you are using, there are no encoding or charset items set up, then set to GBK or gb2312
* Method 2: This is also the method we have been using.
1. In each JSP page set: <%@ page contenttype= "text/html; CHARSET=GBK "%> to tell the server the character set you requested.
2. Defined in the head of each JSP page: <meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "> To tell the browser the character set you are using.
Summary: The most common way to use Sitemesh:
1. Configure the environment,
2. Describe the wrapper you will build in Web-info/decroators.xml.
3. Develop the wrapper described in Decroators.xml, preferably in the/_decorators directory
4.ok, can see the hard work of the results
Usage of decorators.xml (RPM)