Simple setup of spring-mvc Framework and spring-mvc

Source: Internet
Author: User

Simple setup of spring-mvc Framework and spring-mvc

1. Download spring package, http://www.springsource.org/download

I got a spring-framework-4.0.6.RELEASE.

2. Create a web project and import the. jar package in/WebRoot/lib (/spring-framework-4.0.6.RELEASE/libs directory)

Spring framework Configuration

1. web. xml configuration

<? 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"> <! -- Apply the context configuration file --> <context-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/spring-servlet.xml </param-value> </context-param> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- Configure servlet core servlet --> <servlet-name> spring </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <! -- If url-pattern is set to/without the file suffix, other static files (such as js and css) cannot be accessed. For example, Set *. do, the access to static files is not affected. --> <servlet-mapping> <servlet-name> spring </servlet-name> <url-pattern>/</url-pattern> </ servlet-mapping> </web-app>

 

2. Application context profile, spring-servlet.xml (web-defined storage path:/WEB-INF/spring-servlet.xml)

<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 http://www.springframework.org/schema/beans/spring-beans-3. 0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -- 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 = "sendi. znwg. rest "> </context: component-scan> <bean class =" org. springframework. web. servlet. view. internalResourceViewResolver "p: prefix ="/WEB-INF/view "p: suffix = ". jsp "> </bean> </beans>

3. Demo example

Create RestController. java based on the package path (sendi. znwg. rest) configured by spring-servlet.

@Controllerpublic class RestController {    public RestController(){            }        @RequestMapping(value = "/login/{user}",method=RequestMethod.GET)    public ModelAndView rest(HttpServletRequest request,            HttpServletResponse response,@PathVariable("user")String user,ModelMap modelMap){        modelMap.put("loginUser", user);        return new ModelAndView("/login/hello",modelMap);    }        @RequestMapping(value="/welcome",method = RequestMethod.GET)    public String registPost(){        return "/welcome";    }    }

Customizable jsp (view path/WEB-INF/view)

Hello. jsp

<% @ Page import = "java. util. date "%> <% @ page language =" java "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Access after tomcat deployment:

Http: // localhost: 8080/spring-mvcDay2/login/gzsendi

 


How to Build the Srping framework in the Spring framework example?

This book aims to guide readers how to use the Spring framework to develop various effective applications (from simple Web applications to complex enterprise-level applications ). A complete sample application is provided throughout the book. Examples show how Spring provides a clear, layered architecture, how to use Spring to implement good OO design and architecture design, and how to apply best practices.
This book is intended for Java/J2EE architecture designers and developers who want to learn the Spring framework in depth, so that they can effectively use the Spring framework from simple Web applications to complex enterprise-level applications} is also very suitable for beginners of the Spring framework as a reference book.

How to build a spring framework?

First of all, java Beginners should not be eager to learn spring. spring is hard to understand if they have not written jsp + servlet-only projects. In addition, the above framework will be used in the project to greatly limit people's thinking, so you should learn the mvc development mode of jsp + servlet first.
To enter spring, you must first integrate the development environment. If the development environment is difficult to build (such as Eclipse and those who advocate notepad development), you will lose interest in spring, therefore, we recommend that you use Netbeans, which comes with spring2.5 in version 6.1. You can add spring mvc support when creating a web project. All the required packages are included.
The use of spring is relatively simple, so it focuses on understanding the principle. We use spring to actually use other people's design methods. If we do not clarify it, it will become a hard work of the program. You can search for tutorials online, but not all of them. It is better to buy books at first, and you can search for documents online to expand your knowledge later. At the beginning, I read "Detailed description of Integrated ssh Application Development". This book is generally used to get started by choosing one with a certain thickness.

Related Article

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.