SPRING-MVC Hello World (1)

Source: Internet
Author: User

I study a program, all start from the demo.

I don't know the principle of SPRING-MVC first, but some small concepts still need to be understood. Due to previous experience in SSH engineering, the basic stuff is not in the narrative.

1. Preparation environment

Jar Package: These are the red ones. PS: Previously forgot to write, I integrated MyBatis and MySQL, so jar package more

2, JRE environment.

This needs to be noted, as if this version of spring does not support 1.8. Mine is 1.7 of the environment. Since the previous upgrade to 1.8 but the problem, and then dropped to 1.7.

3. Write Spring-mvc.xml configuration file

The following code, which has a specific description, no longer one by one narration

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"xmlns:p= "http://www.springframework.org/schema/p"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-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/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd ">        <!--Scan Controller (Controller layer injection) -<!--<context:component-scan base-package= "Com.demo.controller"/> -             <!--start the annotation-driven Spring MVC feature, register the request URL and annotate the map of the Pojo class method -    <Mvc:annotation-driven/>    <!--Start the package scan feature to register classes with annotations such as @controller, @Service, @repository, @Component, as spring beans -    <Context:component-scanBase-package= "com.demo.*" />       <!--Configure the return value JSON -   <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">           < Propertyname= "Messageconverters">               <List>                   <refBean= "Jacksonmessageconverter"/>               </List>           </ Property>   </Bean>   <!--avoid IE in AJAX request, return JSON appears download -   <BeanID= "Jacksonmessageconverter"class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">                < Propertyname= "Supportedmediatypes">            <List>                <value>Text/html;charset=utf-8</value>                <value>Application/json;charset=utf-8</value>            </List>        </ Property>    </Bean>       <!--add a prefix to a model view -     <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"P:prefix= "/web-inf/jsps/"P:suffix= ". jsp"/>      <!--input parameter type support -   <Mvc:annotation-driven>        <mvc:message-converters>            <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"/>            <Beanclass= "Org.springframework.http.converter.ResourceHttpMessageConverter"/>            <Beanclass= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>        </mvc:message-converters>    </Mvc:annotation-driven></Beans>

4. Start with the Web. xml file

In the Web. xml file, add

    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <!--Configuring the Spring core servlet -    <servlet>        <Servlet-name>Spring</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>            <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>Classpath:config/application/spring-mvc.xml</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>        <!--Url-pattern is configured to/, without a file suffix, which can cause other static files (JS,CSS, etc.) to be inaccessible. If it is *.do, it does not affect the access of static files. -    <servlet-mapping>        <Servlet-name>Spring</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping>

Note the path

5. Backstage Code part

I need a controller, and I'm going to use annotations here.

The source code is:

@Controllerpublic class Indexaction {   //@Autowired//Private userservice userservice;        @RequestMapping ("/index") public    String Index ()    {       //Logtool.logtool (Indexaction.class). Error ("Hello World. "); /This can be commented out       //user User=userservice.checklogin ("1", "1");///       System.out.println (User);//        return "/ Index11 ";    }}    

An interface:

6. Run the test

In the browser, enter: Http://localhost:8080/Spring-mvc/index

Success!

SPRING-MVC Hello World (1)

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.