SpringMVC Hello World, springmvchello

Source: Internet
Author: User

SpringMVC Hello World, springmvchello
Preface

At the beginning of the new year, Lantern Festival, bright outside the window, the sound of firecrackers. For the first time, a person has been in Beijing for the 15th day.

After receiving the holiday, I want to catch up with the project progress and learn java. so wrote the first SpringMVC Hello World article during the Lantern Festival.

 

Development Environment

MyEclipse 10, Tomcat 7, and jdk1.7

 

Project Structure and Spring dependent jar package

Maven is not used at the beginning to learn Spring.

 

Configuration File

When Spring is developed, configuration files are indispensable, such as web. xml, spring xml, and spring mvc xml.

 

Web. xml

Configure spring mvc front-end controller DispatcherServlet and intercept request url-pattern

<! -- Listen to spring context --> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <context-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/root-context.xml </param -value> </context-param> <! -- Configure Spring MVC DispatcherServlet --> <servlet-name> MVC </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <! -- Load spring mvc xml to Spring context --> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/mvc-context.xml </param-value> </init-param> </servlet> <! -- Configure Spring DispatcherServlet interceptor --> <servlet-mapping> <servlet-name> MVC </servlet-name> <url-pattern>/</url-pattern> </servlet- mapping>

  

Spring mvc xml

<Beans: beans xmlns = "http://www.springframework.org/schema/mvc" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: beans = "http://www.springframework.org/schema/beans" xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xsi: SC HemaLocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context Hema/context/spring-context.xsd "> <! -- Load the global configuration file of Spring --> <beans: import resource = "root-context.xml"/> <! -- SpringMVC configuration --> <context: component-scan base-package = "com. autohome. controller"/> <mvc: annotation-driven/> <! -- Configure the SpringMVC view Renderer with the prefix/suffix. jsp renders the view to/page/<method return value>. jsp --> <beans: bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "p: prefix ="/views/"p: suffix = ". jsp "> </beans: bean> </beans: beans>

  

Root-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd                  http://www.springframework.org/schema/context                  http://www.springframework.org/schema/context/spring-context-3.2.xsd                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">                 </beans>

  

Write the first Controller

Using annotations for Controller development mainly uses attribute Controller and RequestMapping

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/Home")public class HomeController {@RequestMapping("Index")public ModelAndView index(){ModelAndView view =new ModelAndView("index");return view;}}

The ModelAndView object is defined in the method. This object is used to specify the view to be rendered as index, and finally the ModelAndView is returned to render the page to index. jsp. Views view folder I put in the WEB-INF directory, jsp has no content, do not paste code, directly create.

OK. Deploy tomcat. Set the first Spring MVC.

 

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.