Web framework-Spring-MVC Environment Construction

Source: Internet
Author: User

Spring framework jar package

Spring address:

Note that the dependency packages of the same version are not available for download after version 3.0.3 on the official website.

Introduce all the following three packages in the dist directory
Org. springframework. web. struts-3.1.0.RELEASE.jar
Org. springframework. spring-library-3.1.0.RELEASE.libd
Org. springframework. web. portlet-3.1.0.RELEASE.jar
Introduce the dependency package com.springsource.org. apache. commons. logging-1.1.1.jar and com.springsource.org. aopalliance-1.0.0.jar

Spring framework Configuration

[Html]
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Web-app
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xmlns = "http://java.sun.com/xml/ns/javaee"
  5. Xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. Id = "WebApp_ID"
  8. Version = "3.0">
  9. <Context-param>
  10. <Param-name> contextConfigLocation </param-name>
  11. <! -- Application context configuration file -->
  12. <Param-value>/WEB-INF/spring-servlet.xml </param-value>
  13. </Context-param>
  14. <Listener>
  15. <Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
  16. </Listener>
  17. <! -- Configure spring core servlet -->
  18. <Servlet>
  19. <Servlet-name> spring </servlet-name>
  20. <Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
  21. <Load-on-startup> 1 </load-on-startup>
  22. </Servlet>
  23. <! -- If the url-pattern is set to/without the file suffix, other static files (such as js and css) cannot be accessed. If it is configured with *. do, the access to static files is not affected -->
  24. <Servlet-mapping>
  25. <Servlet-name> spring </servlet-name>
  26. <Url-pattern>/</url-pattern>
  27. </Servlet-mapping>
  28. </Web-app>

Spring-servlet.xml is configured to enable annotation-based springMVC function, according to the web. xml settings, the path is under the WEB-INF

[Html]
  1. <Beans xmlns = "http://www.springframework.org/schema/beans"
  2. Xmlns: context = "http://www.springframework.org/schema/context"
  3. Xmlns: p = "http://www.springframework.org/schema/p"
  4. Xmlns: mvc = "http://www.springframework.org/schema/mvc"
  5. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  6. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
  7. >
  8. <! -- Start the annotation-driven Spring MVC function and register the ing between the request url and the annotation POJO class method -->
  9. <Mvc: annotation-driven/>
  10. <! -- Enable the package scan function to register classes with annotation such as @ Controller, @ Service, @ repository, and @ Component to become spring beans -->
  11. <Context: component-scan base-package = "com. mvc. rest"/>
  12. <! -- Parse the Model View name and add the prefix and suffix to the model view name upon request -->
  13. <Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver" p: prefix = "/WEB-INF/view/" p: suffix = ". jsp"/>
  14. </Beans>

Demo
1. Create a Constroller Based on the package path (com. mvc. rest) configured in the spring-servlet.xml

[Java]
  1. Package com. mvc. rest;
  2. Import javax. servlet. http. HttpServletRequest;
  3. Import javax. servlet. http. HttpServletResponse;
  4. Import org. springframework. stereotype. Controller;
  5. Import org. springframework. ui. ModelMap;
  6. Import org. springframework. web. bind. annotation. PathVariable;
  7. Import org. springframework. web. bind. annotation. RequestMapping;
  8. Import org. springframework. web. bind. annotation. RequestMethod;
  9. Import org. springframework. web. servlet. ModelAndView;
  10. @ Controller
  11. Public class RestConstroller {
  12. Public RestConstroller (){}
  13. @ RequestMapping (value = "/login/{user}", method = RequestMethod. GET)
  14. Public ModelAndView myMethod (HttpServletRequest request, HttpServletResponse response,
  15. @ PathVariable ("user") String user, ModelMap modelMap) throws Exception {
  16. "LoginUser", user );
  17. Return new ModelAndView ("/login/hello", modelMap );
  18. @ RequestMapping (value = "/welcome", method = RequestMethod. GET)
  19. Public String registPost (){
  20. Return "/welcome ";

Welcome. jsp: The hello. jsp code is as follows:

[Html]
  1. <% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
  2. <%
  3. Path = request. getContextPath ();
  4. BasePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
  5. >
  6. >
  7. <Html>
  8. <Head>
  9. <Base href = "<% = basePath %>">
  10. <Title> My JSP 'hello. jsp 'starting page </title>
  11. <Meta http-equiv = "pragma" content = "no-cache">
  12. <Meta http-equiv = "cache-control" content = "no-cache">
  13. </Head>
  14. <Body>
  15. <% = Request. getAttribute ("loginUser") %>. The current time is <% = new Date () %>
  16. </Body>
  17. </Html>

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.