SSM framework about using JSP as a view to demonstrate problem solving solution

Source: Internet
Author: User

JSP as a view layer to display data, has been a long time, whether in school or enterprise work, will always more or less contact this. Especially for some traditional small and medium-sized enterprises or some years ago, there are many use of JSP as a view presentation layer.

The nature of JSP is that Servlet,servlet can be said to be JSP.

The diagram description is as follows:

With these two graphs, it is sufficient to know that both the JSP and the servlet are finally displayed as HTML in the front end.

You can refer to this tutorial for JSP and Servlet learning tutorials: http://www.runoob.com/jsp/jsp-tutorial.html

JSP presents the data as a view, usually with a few questions?

1. If the SPRINGMVC interception condition is *.do, you can not consider the problem of static resource interception, if it is/, it will have to be considered. Typically, the solution is as follows:

In the SPRINGMVC configuration file, write:

     <!--using Annotations -    <Mvc:annotation-driven/>    <mvc:resources Location= "/static/css/"Mapping= "/css/**"/>    <mvc:resources Location= "/static/images/"Mapping= "/images/**"/>    <mvc:resources Location= "/static/js/"Mapping= "/js/**"/>    <mvc:resources Location= "/static/layer/"Mapping= "/layer/**"/>    <mvc:resources Location= "/static/layui/"Mapping= "/layui/**"/>

Or in the case of Web. Xml.

 <!--Static Resource Release -  <servlet-mapping>    <Servlet-name>Default</Servlet-name>    <Url-pattern>/view/</Url-pattern>    <Url-pattern>/assets/</Url-pattern>    <Url-pattern>/components/*</Url-pattern>    <Url-pattern>/header/*</Url-pattern>    <Url-pattern>/layouts/*</Url-pattern>    <Url-pattern>/snippets/*</Url-pattern>     <Url-pattern>/swagger-ui/*</Url-pattern>     <Url-pattern>*.html</Url-pattern>  </servlet-mapping>

Requires SPRINGMVC configuration file supplement <mvc:default-servlet-handler/>

If this is the case, it is important to note that it must be placed in front of the front controller of the SPRINGMVC, otherwise it will not work.

2. Using the JSP view parser, the path problem requires attention

Workaround: On the Web. XML configuration or directly on 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.

There is a new two class to define the global path, but also can solve the problem.

Baselistener.java

 PackageCom.blog.listener;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;/*** Application Lifecycle Listener Implementation class Baselistener **/ Public classBaselistenerImplementsServletcontextlistener {/*** Default constructor. */     PublicBaselistener () {//TODO auto-generated Constructor stub    }    /**     * @seeservletcontextlistener#contextdestroyed (servletcontextevent)*/     Public voidcontextdestroyed (servletcontextevent arg0) {//TODO auto-generated Method Stub    }    /**     * @seeservletcontextlistener#contextinitialized (servletcontextevent)*/     Public voidcontextinitialized (servletcontextevent arg0) {//TODO auto-generated Method Stub    }    }
Serverstartuplistener.java
 PackageCom.blog.listener;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletContextEvent;/*** Get Web App path *@authorYC **/ Public classServerstartuplistenerextendsBaselistener {@Override Public voidcontextinitialized (Servletcontextevent sce) {//Web App ObjectsServletContext application=Sce.getservletcontext (); //get web App pathString Path =Application.getcontextpath (); //Save the Web app path to the app scopeApplication.setattribute ("App_path", path); }    }

and configure the following listener in Web. XML:

       < Listener >    < Listener-class >com.blog.listener.ServerStartupListener</listener-class>  </ Listener >

The listener is best placed under the Org.springframework.web.context.ContextLoaderListener listener.

3. Questions about using the JSTL tag library

Note to refer to this MAVEN dependency

<Dependency><groupId>Jstl</groupId><Artifactid>Jstl</Artifactid><version>1.2</version></Dependency>

As for why to use Jstl, simple to say than <%%> simple and convenient, relatively flexible, more powerful function.

Of course, it is recommended not to misuse, the cost of abuse will lead to maintenance is very difficult.

If your jstl is using version 1.0, you can refer to this link if you have problems that you cannot use: 53580954

4. View resolver Issues

View resolution is generally not a problem, unless the path is incorrectly written, or there is a problem with the Tomcat application server. Otherwise, the correct view parser path is normally configured and the view data is parsed and returned.

SPRINGMVC configuration file, the correct configuration content is as follows:

    <!--resolution of the Model view name, that is, adding a prefix to the Model view name -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/pages/" />        < Propertyname= "suffix"value= ". jsp" /><!--can be empty, easy to implement the self-based extension to select the view to interpret the logic of the class -    </Bean>

If you put a JSP under Web-inf, you can write this:

    <!--resolution of the Model view name, that is, adding a prefix to the Model view name -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/view/" />        < Propertyname= "suffix"value= ". jsp" /><!--can be empty, easy to implement the self-based extension to select the view to interpret the logic of the class -    </Bean>

As for why put under the Web-inf, because the user can not directly through the browser input URL to access the corresponding JSP file or other resources, it is easy to say that this is conducive to security.

5. Regarding the user information preservation session problem, whether it is servlet (JSP) or in Springmvc, you can define the session's properties and their values and get the properties and their values by the following code

HttpSession session=request.getsession (); Session.setattribute ("User", user);
HttpSession Session  = Request.getsession (); Session.getattribute ("name");------get the message that the session was put in. Session.getattributenames ()-----Get all the information names in the session. Session.getmaxinactiveinterval ()----Get the maximum survival time of the session. Seesion.getid ();------Get the ID of the session. More useful properties can be used to implement a shared session. Session.getcreationtime ()-----Session creation time.

Summary:

This article is mainly about the JSP view parser in the actual development of common problems, for everyone to refer to solve some problems

  

SSM framework about using JSP as a view to demonstrate problem solving solution

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.