Simple example of spring MVC

Source: Internet
Author: User

A simple SPRINGMVC instance

Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.muyang</groupId> <artifactid>gu2</artifactid > <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>gu2 Maven webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version> 4.3.5.release</spring.version> </properties> <dependencies> <!--Spring Module Library--< Dependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-BEANS&LT;/ARTIFAC tid> <version>${spring.version}</version> </dependency> <dependency> <gr Oupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> &LT;DEP Endency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</artifacti D> <version>${spring.version}</version> </dependency> <!--https://mvnrepository.com /ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-TX-<!--transaction transactions-<dependency> <groupid&gt ;org.springframework</groupid> <artifactId>spring-tx</artifactId> <version>${spring. Version}</version> </dependency> <!--servlet Dependencies--<!--servlet (H        Ttpservletrequest,httpservletresponse)-<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependenc Y> <!--JsTL for JSP page-<!--https://mvnrepository.com/artifact/jstl/jstl--<dependency> <gro    Upid>jstl</groupid> <artifactId>jstl</artifactId> <version>1.2</version>  </dependency> <!--JSP API--<!--Http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -<dependency> <groupId>javax.servlet.jsp</groupId> &LT;ARTIFACTID&GT;JSP-API&L t;/artifactid> <version>2.2</version> <scope>provided</scope> </dependen cy> </dependencies> <build> <finalName>gu2</finalName> < /build></project>

  

Xml

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "><web-app> <display-name>archetype Created Web application</display-name> < Context-param> <param-name>contextConfigLocation</param-name> <param-value>/web-inf/        applicationcontext.xml</param-value> </context-param> <listener> <listener-class>    Org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> ORG.SPRINGFRAMEWORK.WEB.SERVLET.D           Ispatcherservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern&  Gt </servlet-mapping> </web-app&Gt 

  

Springmvc-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" 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/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context.xsd "> <!--Scan package, automatically introduce bean--><context:component-scan base-package=" Springmvc "/> <!--View-- ><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= " Prefix "> <value>/WEB-INF/views/</value> </property>   <property name= "suffix" > <value>.jsp</value> </property></bean> </beans> 

  

Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" 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/SCHEMA/MVC                    http://www.springframework.org/schema/mvc/spring-mvc.xsd/                    http Www.springframework.org/schema/context                    http://www.springframework.org/schema/context/spring-context.xsd " >    </beans>

  

Hellocontroller.java

Package Springmvc.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controllerpublic class Hellocontroller {@RequestMapping ( Value= "/hello", method=requestmethod.get) public    String Hello ()    {              return "index";    }}

  

Jsp

<%@ 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" >

  

Simple Spring MVC instance

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.