Spring MVC Starter Demo

Source: Internet
Author: User

1. Web. XML configuration

<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1">  <context-param>--spring Controller configuration <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param>    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener><servlet>--spring MVC default Schedule <servlet-name>dispatcher</servlet-name> <servlet-c Lass>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name&        Gt <url-pattern>/</url-pattern> </servlet-mapping>    <servlet-mapping>        <Servlet-name>Sh</Servlet-name>        <Url-pattern>/servlet/sh</Url-pattern>    </servlet-mapping></Web-app>

2, Dispatcher-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"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/conte Xt/spring-context-3.0.xsd ">    <Context:component-scanBase-package= "Com.javahash.spring.controller"></Context:component-scan>--Configure Scan packages    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value=""></ Property>--Path prefix        < Propertyname= "suffix"value= ". jsp"></ Property>--path suffix    </Bean></Beans>

3. Controller Code

 PackageCom.javahash.spring.controller;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportJava.util.logging.Logger;/*** Created by Administrator on 2014/11/8.*/@Controller Public classHelloworldcontroller {@RequestMapping ("/hello")     PublicString Hello (@RequestParam (value= "name", required=false, defaultvalue= "World") String name, model model) {Model.addattribute ("Name", name); return"HelloWorld"; }}

Spring MVC Starter Demo

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.