Getting Started with Spring MVC

Source: Internet
Author: User

1. Spring MVC
Like struts, it's an MVC framework, a bit similar to Strusts2, and spring seamlessly connected to a framework of spring.
2. Environment Construction
1) Create a new Web project in Eclipse
2) Add in Web. xml

  <Servlet>    <Servlet-name>Springmvc</servlet-name>    <Servlet-class>Org.Springframework.Web.Servlet.Dispatcherservlet</servlet-class>    <Init-param>    <Param-name>Contextconfiglocation</param-name>    <Param-value>Classpath*: config/spring-servlet.XML</param-value>    </init-param>    <Load-on-startup>1</load-on-startup>   </servlet>   <Servlet-mapping>    <Servlet-name>Springmvc</servlet-name>    <Url-pattern>/</url-pattern>   </servlet-mapping> 

3) Import the required jar packages

4) Edit the Web. xml file

<?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" xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_ 5.xsd "id=" webapp_id " version=" 2.5 ">                <display-name>Springmvc</display-name>    <welcome-file-list>        <welcome-file>Index.html</welcome-file>    </welcome-file-list>    <servlet>        <servlet-name>Springmvc</servlet-name>        <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>Contextconfiglocation</param-name>            <param-value>Classpath:spring-mvc.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>Springmvc</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping></Web-app>

5) Edit the Spring-mvc.xml file

<?xml version= "1.0" encoding= "UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http// Www.springframework.org/schema/context "xsi:schemalocation=" http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/context http://www.springframework.org/schema/context/spring-context.xsd ">                    <!--packages that use annotations, including subsets--    <context:component-scan base-package="Com.model"/>    <!--View resolver --    <bean id= "viewresolver"class=" Org.springframework.web.servlet.view.InternalResourceViewResolver ">            < property name="prefix" value="/web-inf/jsp/" / >        < property name="suffix" value=". jsp"></Property >    </Bean></Beans>

6) Hellospring.java

package  Com.model; import  Org.springframework.stereotype.Controller; import  Org.springframework.ui.Model; import  org.springframework.web.bind.annotation.requestmapping; @Controller  public  class  helloworldcontroller  {  @RequestMapping  ("/helloworld ") public  String helloworld  (model model) {Model.addattribute ( " Message ", " Hello springmvc! "); return   "HelloWorld" ; }}

Getting started with 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.