SpringMVC-a simple example: springmvc

Source: Internet
Author: User

SpringMVC-a simple example: springmvc

1. Import the jar package

2. Configure the web. xml file

<servlet>  <servlet-name>dispatcherServlet</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  </init-param></servlet><servlet-mapping>  <servlet-name>dispatcherServlet</servlet-name>  <url-pattern>*.action</url-pattern></servlet-mapping>

Note:

The core processor class org. springframework. web. servlet. DispatcherServlet of SpringMVC is configured. url-pattern specifies the request to be filtered.

Note that DispatcherServlet loads the SpringMVC Config file for/WEB-INF/servletName-servler.xml by default, and its location and name can be modified through initialization parameters.

3. Add SpringMVC Config file (springmvc-servlet.xml)

<context:component-scan base-package="com.nucsoft.springmvc.handler"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/views/"/>  <property name="suffix" value=".jsp"/></bean>

Note:

(1) Specify the base package to be scanned

(2) Specify the view Parser: prefix + viewName + suffix

4. Add Target classes

/** * @author solverpeng * @create 2016-08-01-19:19 */@Controllerpublic class HelloWorldHandler {    @RequestMapping("/hello")    public String hello() {        System.out.println("hello world...");        return "hello";    }}

Note:

(1) The request processing class must be in the IOC container

(2) @ RequestMapping is used to map requests. The value Attribute specifies the mapped url. It is equivalent to namespace.

(3) The returned value is eventually parsed as a ModelAndView object. Return to the view with the view parser.

 

5. SpringMVC, as a presentation layer framework, is another excellent framework after Stuts2 and is supported by the entire Spring system. It feels more elegant to use.

When learning spring MVC, you must understand its core processor and the issues to be solved by the presentation layer,

For example: obtain native Servlet resource problems, ing problems, parameter acquisition problems, model injection problems, formatting problems, Data Validation Problems, return value processing problems, view rendering problems, prevent form repeated submission problems, files upload and download problems.

SpringMVC's entire running process and request lifecycle issues. Can we achieve what we want through a custom method, but SpringMVC has not yet solved the problem.

What are the advantages and disadvantages of Struts2.

To learn SpringMVC, you must understand the above problems.

 

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.