SSM framework about using JSP as a view to show the problem solution
JSP has been used as a view layer to display data for a long time. No matter studying in school or working in an enterprise, it will always contact this more or less. Especially for some traditional small and medium-sized enterprises or some enterprises a few years ago, many use JSP as the view display layer.
JSP is essentially a servlet, which can be said to be JSP.
The description is as follows:
Through the above two figures, it is enough to know that both JSP and servlet are finally displayed in the front end in the form of HTML.
For the learning tutorial of JSP and servlet, you can refer to this tutorial: http://www.runoob.com/jsp/jsp-tutorial.html
How many problems can JSP display data as a view?
1. If it is spring MVC, the interception condition is * Do, the interception of static resources can not be considered. If yes / Yes, it has to be considered. Usually, the solution is as follows:
Write in the spring MVC configuration file:
Or on the web That's what XML says
Spring MVC configuration file is required to supplement < MVC: default servlet handler / >
If it is used in this way, it should be noted that it must be placed in front of the front-end controller of spring MVC, otherwise it will not work.
2. The path problem needs attention when using JSP view parser
Solution: on the web XML configuration or directly in the JSP page for each static resource/ Or.. // Replace with ${pagecontext. Request. Contextpath}
${pagecontext. Request. Contextpath}, because the context path is obtained directly through it.
In addition, creating two classes to define global paths can also solve this problem.
BaseListener. java
ServerStartupListener.java
And on the web XML can be configured as follows:
The listener is best placed at org springframework. web. context. Under the contextloaderlistener listener.
3. About using JSTL tag library
Note that this Maven dependency should be referenced
As for why to use JSTL, it is simpler and more convenient than <%% >, relatively flexible and powerful.
Of course, it is recommended not to abuse. The cost of abuse will make it very difficult to maintain.
If you use version 1.0 of JSTL, you can refer to this link if you encounter problems that cannot be used: https://blog.csdn.net/whh743/article/details/53580954
4. View parser problem
There is generally no problem with view parsing, unless the path is written incorrectly or there is a problem with the Tomcat application server. Otherwise, if the correct view parser path is configured, the view data will be parsed and returned normally.
Spring MVC configuration file. The correct configuration contents are as follows:
If the JSP is placed under WEB-INF, it can be written as follows:
As for why it is placed under WEB-INF, the reason is that users cannot directly access the corresponding JSP file or other resources by entering the URL through the browser. In short, this is conducive to security.
5. For the problem of saving user information session, whether in servlet (JSP) or in sprigmvc, you can define the session attribute and its value and obtain the attribute and its value through the following code
Summary:
This paper mainly focuses on the common problems of JSP view parser in practical development, for your reference to solve some problems