Springmvc IDEA, springmvcidea

Source: Internet
Author: User

Springmvc IDEA, springmvcidea

Looking back at the Web development process on the Java platform, starting from the emergence of Servlet, to the flourishing of JSP, then the Servlet + JSP era, finally evolved into the era where Web development frameworks are prevalent. When I come into contact with a new Web framework, I would like to ask where is the framework's advantage? Or where is it better than other frameworks? If the Spring MVC framework is not used, but other frameworks are used and can meet the requirements well, it may not be a good idea to change the framework. Spring MVC should be given priority if I first came into contact with the Web development framework like me.

  • In terms of framework processors, Spring MVC rationally and completely separates various concerns during request processing, the response role is clearly set for modeling and handling various concerns throughout the Declaration cycle. These include HandlerMapping, LocaleResolver, and ViewResolver for flexible view selection.
  • From the presentation layer, Spring MVC uses a logical naming View policy. By introducing ViewResolver and View, it clearly separates the selection of View policies and the coupling between rendering and specific controllers, it is applicable to various view technologies that can be easily integrated into Spring MVC, whether JSP/JSTL is used as view technology, Velocity/FreeMarker, or even binary view formats such as PDF/Excel, you only need simple configuration.
  • Spring MVC also has another feature: "famous teachers". As a member of the Spring family, it is easy to get support from other brothers in the family, from IoC to AOP, data access layer and transaction management layer support. Spring MVC is a request-Driven Web framework that uses a single Servlet as the Front Controller of the entire application. After receiving a specific Web request, the Servlet will refer to the configured ing relationship, forward the pending Web request to the next-level controller for processing.
Create a Spring MVC Project

The development environment of LZ is IDEA. From the perspective of using IDEA over the past few days, the overall interface of IDEA is much faster than that of eclipse, and the speed of opening and closing is much faster. However, I still don't know where the many features are, whining...

1. Open IDEA and create a project

2. Select the project as follows:

Of course, you can also select Spring-Spring MVC when creating a project (here you also need to select Web Application)

3. Name the project mvcdemo, as shown below, and click finash.

4. After the new project is created, you need to manually add the associated jar package (Why does LZ not use maven, because it does not ...), Right-click the project name and choose Open Module Settings to add the corresponding Jar package.

5. The project is as follows. For convenience, add all jar packages of spring and the commons-logging package.

6 configure the web. xml file and mvcdemo-servlet.xml File

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="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">    <display-name>Spring MVC</display-name>    <servlet>        <servlet-name>mvcdemo</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>mvcdemo</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

The above is web. the xml file, DispatcherServlet, as the Front Controller of the entire application, is used to process all requests, rather than obtaining URL ing matching support from Web containers like "one Web request corresponds to one Servlet, instead, you have to handle the ing between the specific Web request and the specific processing class, that is, you need to use ControllerBeanNameHandlerMapping.

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" 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.xsd"> <! -- HandlerMapping locate the corresponding controller according to benaname --> <bean class = "org. springframework. web. servlet. mvc. support. ControllerBeanNameHandlerMapping"/> <! -- Controller configuration Processor --> <bean name = "/hello" class = "com. luoxn28.hello. HelloController"/> <! -- ViewResolver view parser --> <bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> <! -- Prefix and suffix --> <property name = "prefix" value = "/WEB-INF/"/> <property name = "suffix" value = ". jsp "/> </bean> </beans>

ControllerBeanNameHandlerMapping is used to process the ing between a specific Web request and a specific processing class. Controller (HelloController is the Controller implementation class) is also a specific processing class. InternalResourceViewResolver configures view resolution information.

7. Create a com. luoxn28.hello package in the src folder, and then create a HelloController class under the package.

package com.luoxn28.hello;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloController extends AbstractController {    @Override    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {        String hello = request.getParameter("hello");        System.out.println("HelloController: " + hello);        ModelAndView mav = new ModelAndView("hello");        mav.addObject("hello", hello);        return mav;    }}

8. Create a hello. jsp file in the WEB-INF folder with the following content:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

9 change the index. jsp file as follows:

<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> 

10 so far, the entire project has been established. The current project view is as follows:

 

Then move the project name right-click Open Module Settings, configure, add Spring under Modules, select Spring Application Context as the WEB-INF under the mvc-demo-servlet.xml, as shown in:

Click Artifacts. The following message is displayed. Click the Fix... button, as shown below:

After all settings are complete, click the start button. The screen is shown as follows:

Enter a string and click Submit. For example, enter "luoxn28". The following page is displayed. The project ends.

 

References

1. Spring MVC video tutorials from getting started to proficient

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.