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]
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Web-app
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xmlns = "http://java.sun.com/xml/ns/javaee"
- Xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- Id = "WebApp_ID"
- Version = "3.0">
- <Context-param>
- <Param-name> contextConfigLocation </param-name>
- <! -- Application context configuration file -->
- <Param-value>/WEB-INF/spring-servlet.xml </param-value>
- </Context-param>
- <Listener>
- <Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
- </Listener>
- <! -- Configure spring core servlet -->
- <Servlet>
- <Servlet-name> spring </servlet-name>
- <Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
- <Load-on-startup> 1 </load-on-startup>
- </Servlet>
- <! -- 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 -->
- <Servlet-mapping>
- <Servlet-name> spring </servlet-name>
- <Url-pattern>/</url-pattern>
- </Servlet-mapping>
- </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]
- <Beans xmlns = "http://www.springframework.org/schema/beans"
- Xmlns: context = "http://www.springframework.org/schema/context"
- Xmlns: p = "http://www.springframework.org/schema/p"
- Xmlns: mvc = "http://www.springframework.org/schema/mvc"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: schemaLocation = "http://www.springframework.org/schema/beans
- >
- <! -- Start the annotation-driven Spring MVC function and register the ing between the request url and the annotation POJO class method -->
- <Mvc: annotation-driven/>
- <! -- Enable the package scan function to register classes with annotation such as @ Controller, @ Service, @ repository, and @ Component to become spring beans -->
- <Context: component-scan base-package = "com. mvc. rest"/>
- <! -- Parse the Model View name and add the prefix and suffix to the model view name upon request -->
- <Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver" p: prefix = "/WEB-INF/view/" p: suffix = ". jsp"/>
- </Beans>
Demo
1. Create a Constroller Based on the package path (com. mvc. rest) configured in the spring-servlet.xml
[Java]
- Package com. mvc. rest;
- Import javax. servlet. http. HttpServletRequest;
- Import javax. servlet. http. HttpServletResponse;
- Import org. springframework. stereotype. Controller;
- Import org. springframework. ui. ModelMap;
- Import org. springframework. web. bind. annotation. PathVariable;
- Import org. springframework. web. bind. annotation. RequestMapping;
- Import org. springframework. web. bind. annotation. RequestMethod;
- Import org. springframework. web. servlet. ModelAndView;
- @ Controller
- Public class RestConstroller {
- Public RestConstroller (){}
- @ RequestMapping (value = "/login/{user}", method = RequestMethod. GET)
- Public ModelAndView myMethod (HttpServletRequest request, HttpServletResponse response,
- @ PathVariable ("user") String user, ModelMap modelMap) throws Exception {
- "LoginUser", user );
- Return new ModelAndView ("/login/hello", modelMap );
- @ RequestMapping (value = "/welcome", method = RequestMethod. GET)
- Public String registPost (){
- Return "/welcome ";
Welcome. jsp: The hello. jsp code is as follows:
[Html]
- <% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
- <%
- Path = request. getContextPath ();
- BasePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
- >
- >
- <Html>
- <Head>
- <Base href = "<% = basePath %>">
- <Title> My JSP 'hello. jsp 'starting page </title>
- <Meta http-equiv = "pragma" content = "no-cache">
- <Meta http-equiv = "cache-control" content = "no-cache">
- </Head>
- <Body>
- <% = Request. getAttribute ("loginUser") %>. The current time is <% = new Date () %>
- </Body>
- </Html>