SPRINGMVC Framework Study Notes (1)--helloworld

Source: Internet
Author: User

Build SPRINGMVC Frame

1. Add a JAR Package

Jsp-api.jar
Servlet-api.jar
Jstl.jar
Commons-logging-1.1.1.jar
Spring-beans-4.1.6.release.jar
Spring-context-4.1.6.release.jar
Spring-core-4.1.6.release.jar
Spring-expression-4.1.6.release.jar
Spring-tx-4.1.6.release.jar
Spring-web-4.1.6.release.jar
Spring-webmvc-4.1.6.release.jar

2. Defining a controller and creating a JSP page

  controller needs to implement controller interface

/*** Custom controller requires implementation of controller interface **/ public classHellocontrollerImplementsController{@Override publicmodelandview handlerequest (httpservletrequest req, httpservletresponse resp)throwsException {//calling a class to handleSystem.out.println ("-----hello controller-------"); //creates a Modelandview object that is finally rendered as the specified view by the view rendererModelandview mv =NewModelandview (); //sets the view name----finally resolves to the specified pageMv.setviewname ("hello"); //Add result data This data can be obtained through the El expression in the pageMv.addobject ("msg", "first Spring MVC app"); returnmv; }}

3. Configuration

You need to configure the Handlermapping mapper and the Handleradapter adapter and conteroller, as well as configure the view renderer

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD ">    <!--Configuring the Handlermapper mapper -    <Beanclass= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>      <!--Configure controller Custom controllers -    <Beanname= "/hello.do"class= "cn.sxt.controller.HelloController"/>    <!--Configuring the Handeradapter adapter -    <Beanclass= "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>    <!--Configure the View renderer -    <BeanID= "jspviewresolver"class= "org.springframework.web.servlet.view.InternalResourceViewResolver">        < propertyname= "viewclass"value= "org.springframework.web.servlet.view.JstlView"/>        <!--prefix the view name after rendering the view -        < propertyname= "prefix"value= "/web-inf/jsp/"/>        <!--suffix of post-rendered view -        < propertyname= "suffix"value= ". jsp"/>        <!--example: The view is named: hello after rendering:/web-inf/jsp/hello.jsp the page -    </Bean></Beans>

4. Code Structure

5. Configure the Project Access path

6. Launch Tomcat Access http://localhost:8080/hello/hello.do

First Spring MVC App

SPRINGMVC Framework Study Notes (1)--helloworld

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.