SPRING-MVC environment Construction of web framework (RPM)

Source: Internet
Author: User

Spring Framework jar Package

1. Download Spring Source Package
Spring Address: Http://www.springsource.org/download
I'm under the Spring-framework-3.1.0.release-with-docs.zip.
Download Dependency Package: Spring-framework-3.0.5.release-dependencies.zip
Note Official on-line 3.0.3 version after the same version dependent package does not provide download

2. Import the required jar package
Introduction of the Dist directory in addition to the following three all the remaining packages
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
Introduction of Com.springsource.org.apache.commons.logging-1.1.1.jar and Com.springsource.org.aopalliance-1.0.0.jar under dependent packages

Spring Framework Configuration

1. Web. XML configuration

[HTML]View PlainCopy
  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 profile --
  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. <!--Configuring the 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. <!--Url-pattern is configured to/, without a file suffix, which can cause other static files (JS,CSS, etc.) to be inaccessible. With *.do, it does not affect access to static files- -
  24. <servlet-mapping>
  25. <servlet-name>spring</servlet-name>
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. </Web-app>

2. Application Context Configuration
Spring-servlet.xml is configured to turn on the annotation-based SPRINGMVC function, as set in Web. XML, the path is Web-inf

[HTML]View PlainCopy
  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. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. Http://www.springframework.org/schema/context
  9. Http://www.springframework.org/schema/context/spring-context.xsd
  10. Http://www.springframework.org/schema/mvc
  11. Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
  12. <!--start the annotation-driven spring MVC feature, the registration request URL and the annotation Pojo class method mapping--
  13. <mvc:annotation-driven />
  14. <!--start the package scan feature to register classes with annotations such as @controller, @Service, @repository, @Component and so on as spring beans--
  15. <context:component-scan base-package="com.mvc.rest" />
  16. <!--resolution of the name of the model view, and the model view name is added before and after the request
  17. <Bean class="Org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix= "/web-inf/view/" p:suffix= ". jsp" />
  18. </Beans>

Demo Example
1. New Constroller based on Spring-servlet.xml configured package path (com.mvc.rest)

[Java]View PlainCopy
  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. Modelmap.put ("Loginuser", user);
  17. return new Modelandview ("/login/hello", Modelmap);
  18. }
  19. @RequestMapping (value = "/welcome", method = Requestmethod.get)
  20. Public String Registpost () {
  21. return "/welcome";
  22. }
  23. }

2. Build JSP View

View path in Spring-servlet.xml configuration (/web-inf/view/), according to the above Restconstroller class, we set up the view directory under Web-inf, set up welcome.jsp and login/under View hello.jsp
Welcome.jsp random, the hello.jsp code is as follows:

[HTML]View PlainCopy
  1. <%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%>
  2. <%
  3. String Path = Request.getcontextpath ();
  4. String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/  ";
  5. %>
  6. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">
  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. Hello:<%=request.getattribute ("Loginuser")%;, now time is <%= new date ()%>
  16. </body>
  17. </html>

3. Deployment Access
Http://localhost:8080/SpringMvcDemo/welcome

http://blog.csdn.net/linxcool/article/details/7094460

SPRING-MVC environment Construction of web framework (RPM)

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.