Spring mvc-Static Page example (reprint practice)

Source: Internet
Author: User
Tags tomcat server tutorialspoint

The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_static_pages.htm

Description: The sample is based on spring MVC 4.1.6.

The following example shows how to use the Spring MVC framework to write a simple web-based application that can access static pages and dynamic pages with the help of the <mvc:resources> tag. First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:

Steps Description
1 Create a project named Helloweb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section.
2 Create a Java class Webcontroller under the Com.tutorialspoint package.
3 Create a static file final.htm under the JSP subfolder.
4 Update the spring configuration file Helloweb-servlet.xml under the Webcontent/web-inf folder as described below.
4 The final step is to create the contents of all the source and configuration files and export the application as described below.

Webcontroller.java

 PackageCom.tutorialspoint;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller Public classWebcontroller {@RequestMapping (value= "/index", method =requestmethod.get) PublicString Index () {return"Index"; } @RequestMapping (Value= "/staticpage", method =requestmethod.get) PublicString Redirect () {return"Redirect:/pages/final.htm"; }}

Helloweb-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5 . xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mv C-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-3.0.xsd ">     <Context:component-scanBase-package= "Com.tutorialspoint" />         <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/jsp/" />        < Propertyname= "suffix"value= ". jsp" />    </Bean>        <mvc:resourcesMapping= "/pages/**" Location= "/web-inf/pages/" />
<Mvc:annotation-driven/> </Beans>

Here you use the <mvc:resources .... /> tag to map a static page. The mapping property must be an ant pattern that specifies the URL pattern for an HTTP request. The location property must be specified as a static page, including one or more valid resource directory locations for pictures, style sheets, JavaScript, and other static content. You can specify multiple resource locations by using a comma-separated list of values.

The following is the contents of the Spring view file web-inf/jsp/index.jsp. This will be a landing page that will send a request to access the staticpage service method, which redirects the request to a static page available in the Web-inf/pages folder.

index.jsp

<%@taglib URI="Http://www.springframework.org/tags/form"prefix="form"%><HTML><Head>    <title>Spring Landing Page</title></Head><Body><H2>Spring Landing Pag</H2><P>Click below button to get a simple HTML page</P><Form:formMethod= "GET"Action= "/helloweb/staticpage"><Table>    <TR>    <TD>    <inputtype= "Submit"value= "Get HTML page"/>    </TD>    </TR></Table>  </Form:form></Body></HTML>

Final.htm

<HTML><Head>    <title>Spring Static Page</title></Head><Body><H2>A Simple HTML Page</H2></Body></HTML>

After you finish creating the source and configuration files, export the application. Right-click the application and use the Export > WAR file option and save your helloweb.war file in the Tomcat WebApps folder.

Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try to access the URL http://localhost:8080/HelloWeb/index. If everything works for your spring Web application, you should see the following results:

Click the "Get HTML page" button to access the static page mentioned in the Staticpage service method. If everything works for your spring Web application, you should see the following results:

Maven Example:

Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test3

Spring mvc-Static Page example (reprint practice)

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.