Springmvc Getting Started

Source: Internet
Author: User

1.SPRINGMVC is a presentation framework produced by spring, and struts2 belong to the same type of framework.

2. Springmvc Effect:
A. Receiving requests and receiving parameters from requests
B. Return the processed data to the page and display it on the page

3.SPRINGMVC three main components: Processor Mapper, processor adapter, view resolver
What we need to write in SPRINGMVC: The processor is the controller we wrote, the view is the page we wrote.

4. If the processor mapper is not configured in SPRINGMVC, the processor adapter can be used, but the system performance is degraded.
If the processor mapper and processor adapters are not configured in Springmvc.xml, the default every request will be to find dispatcherservlet.properties
Configuration file, go inside the processor Mapper list and the processor adapter list, matching each other to find the corresponding type of execution. If the Springmvc.xml
The processor mapper and processor adapter shown in the direct execution will not look for the dispatcherservlet.properties configuration file, it will
Increase the speed of the system.

5. Note driver: The role is to configure this annotation is equivalent to the display configuration of the latest version of the note form of the processor mapper and processor adapter, will enhance the system running speed
6. Annotation driver and annotation scanning, whether it is necessary to configure why, what is the difference?
Must all be configured, note-driven and annotation-scan, different functions,
annotation scanning, which is effective with @controller annotations
Note drivers are equivalent to displaying configuration of the latest version of the processor mapper and processor adapter to speed up system operation.

Spring principle :

        

SPRINGMVC Introductory Case

This tutorial uses the environment:

JDK:jdk1.7.0_72

Eclipse:mars

Tomcat:apache-tomcat-7.0.53

SPRINGMVC:4.1.3

    1. Create a javaweb project
    2. importing jar Packages
    3. Create itemlist.jsp

4. Copy the itemlist.jsp in the reference material to the /web-inf/jsp directory of the project

5. Create Controller

        

@Controller

Public class Itemcontroller {

@RequestMapping ("/itemlist")

Public Modelandview itemList () throws Exception {

list<items> itemList = new arraylist<> ();

Product List

Items Items_1 = new items ();

Items_1.setname ("Who Am I");

Items_1.setprice (6000F);

Items_1.setdetail ("This is the test data");

Itemlist.add (Items_1);

Create a Modelandview object

Modelandview Modelandview = new modelandview ();

Add model

Modelandview.addobject ("ItemList", itemList);

Add a View

Modelandview.setviewname ("/web-inf/jsp/itemlist.jsp");

Modelandview.setviewname ("Itemslist");

return Modelandview;

}

}

Create Springmvc.xml

  

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="Http://www.springframework.org/schema/context"

xmlns:dubbo="Http://code.alibabatech.com/schema/dubbo"

xmlns:mvc="Http://www.springframework.org/schema/mvc"

xsi:schemalocation="Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

Http://code.alibabatech.com/schema/dubbo

Http://code.alibabatech.com/schema/dubbo/dubbo.xsd

Http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd ">

<context:component-scan base-package="Cn.kingdee.springmvc.controller"/>

</beans>

Configuring the front-end controller

  

<!--front-end controller--

<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.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>Springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

Start testing

Note: Parameter binding

A) Springmvc the default supported types:
HttpServletRequest, HttpServletResponse, HttpSession, Model
The default supported parameter is to add when needed, do not need to be free to use.

b) Simple data type (string, double, long, Boolean, etc.)
SPRINGMVC can directly receive these simple types of parameters, and can be automatic type conversion,
The variable name required to be received must be equal to the Name property value of the input box on the page
If you want to receive the variable name arbitrarily named can use @requestparam annotations, generally do not use
@RequestParam ("id") Integer idddddd,

c) Pojo Type:
Requirement: The property name of the received Pojo must be equal to the Name property value of the input box on the page

d) Pojo Package type:
Requirement: The Name property value of the input box of the page must be equal to the accepted Pojo property. property. Properties .....

e) Custom parameter bindings:
The default SPRINGMVC can only automatically convert parameters of simple types, and cannot perform automatic type conversions for date dates.
So we need to manually write a custom converter for the conversion.

Springmvc Getting Started

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.