Java Framework-spring MVC application 001-spring MVC configuration

Source: Internet
Author: User

  Spring MVC Configuration

1. Importing the JAR Package

2. Add configuration about SPRINGMVC in the Web. XML configuration file

<!--Configure the setting of Springmvcdispatcherservlet and configure the mapping-->
<!--spring is divided into multiple files for configuration, where the Init-param attribute is not specified in Servlet-name, and the spring configuration file that the system automatically looks for is [Servlet-name]-servlet.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: springmvc-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. Add springmvc-servlet.xml config file under src

<?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:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!--Scan the Packageand the Sub Package-<context:component-scan base- Package= "Test. Springmvc "/> <!--don T handle the static resource--&LT;MVC:default-servlet-handler/> <!--ifYou use annotation must configure following setting-<mvc:annotation-driven/> <!--Configure the Internalresourceviewresolver-<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"ID= "Internalresourceviewresolver" > <!--prefix--<property name= "prefix" value= "/web-inf/jsp/"/> <!--suffix-<property name= "suffix" value= ". jsp"/> </bean></beans>

4. Create a folder named JSP under the Web-inf folder to hold the JSP view. Create a hello.jsp and add "Hello world" to the body.

5. Set up the package and controller

6. Writing Controller Code

@Controller @requestmapping ("/mvc")  Public class Mvccontroller {    @RequestMapping ("/hello")    public  String Hello () {                 return "Hello";    }}

7. Start the server

Java Framework-spring MVC application 001-spring MVC configuration

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.