[Spring] SpringMVC getting started example, springspringmvc

Source: Internet
Author: User

[Spring] SpringMVC getting started example, springspringmvc

C directory structure:

Contents structure [-]
  • Error
  • References
  • What is SpringMVC?

    The full name of MVC is Model View Controller. By implementing the MVC framework, data, views, and business logic can be well separated. Spring MVC is also a MVC framework, which is a follow-up product of Spring framework. It also requires Spring Jar packages to run.

    SpringMVC Design Principles

    Spring MVC is designed around DispatcherServlet, where DispatcherServlet distributes requests to specific controllers.

    Below is a simple flowchart of SpringMVC:

    SpringMVC Entry Example

    My project structure:

    1. Copy the Jar package

    Copy the Jar package of Spring to the lib directory.

    2. Web. xml file

    Configure the Web. xml file

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">    <!--     this name matched project name   -->  <display-name>SpringMVC</display-name>    <!--     transmit to dispacherServlet to deal with information that passed from table servlet-mapping   -->   <servlet>     <servlet-name>mySpring</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>mySpring</servlet-name>     <url-pattern>*.ms</url-pattern>   </servlet-mapping>      <!--      solve messy code    -->    <filter>          <filter-name>encodingFilter</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <init-param>              <param-name>encoding</param-name>              <param-value>UTF-8</param-value>          </init-param>      </filter>      <filter-mapping>          <filter-name>encodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping> </web-app>
    3, mySpring-servlet.xml files

    Create the dispatcherServlet configuration file [servlet-name]-servlet. xml in the WEB-INF of the project.

    The file name must be in the format of "[servlet-name]-servlet. xml ", for example, the author of the web file servlet-name is mySpring, the file name must be mySpring-servlet.xml, if the wrong location or file name format is not correct, then through the web. the dispatcherServlet file cannot be found in the xml file. It can also be said that as long as the DispatcherServlet is configured in Web. xml, the name of the DispatcherServlet will be uniquely identified. A web. xml file can map multiple DispatcherServlet files.

    <?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:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:jee="http://www.springframework.org/schema/jee"    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">  <!--     configure HandlerMapping   -->   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>  <!--     configure HandlerAdapter   -->   <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>      <!--      configure controller    -->    <bean name="/testSpring.ms" class="com.my.spring.TestSpring"></bean>        <!--       configure viewResolver     -->     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="viewClass">             <value>org.springframework.web.servlet.view.InternalResourceView</value>         </property>        <property name="prefix" value="/view/"></property>       <property name="suffix" value=".jsp"></property>     </bean></beans>
    4. welcome. jsp file
    <% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 5. result. jsp file
    
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">6. TestSpring. java File
    
    package com.my.spring;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class TestSpring extends AbstractController {    @Override    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {        // TODO Auto-generated method stub        String value=request.getParameter("words");                //create model        Map<String,String>model=new HashMap<String,String>();        //set value        model.put("myValue", value);                //create modelAndView        ModelAndView mav=new ModelAndView("result",model);                return mav;    }}

    This file must implement the AbstrcactController abstract class or the Controller Interface. Then override the handlerRequestInternal method. Here, you must select the AbstractController or Controller under the servlet. If the reader selects the portlet and the project does not have the corresponding package, an error will be reported.

    Error 1, error 1:

    Error code:

    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    Solution:

    This is because there is no such class under the project, add servlet-api.jar, see how Eclipse releases Web project.

    2. Error 2:

    Error code:

    Server Tomcat v8.5 Server at localhost failed to start.

    Solution:

    There are many reasons for this error. We recommend that you check whether the syntax in your Web. xml file is correct.

    3. Error 3:

    Error code:

    Multiple markers at this line
        - The type javax.portlet.ActionResponse cannot be resolved. It is indirectly
         referenced from required .class files
        - The type javax.portlet.RenderRequest cannot be resolved. It is indirectly
         referenced from required .class files
        - The type javax.portlet.RenderResponse cannot be resolved. It is indirectly
         referenced from required .class files
        - The type javax.portlet.ActionRequest cannot be resolved. It is indirectly
         referenced from required .class files
        - The type javax.portlet.PortletContext cannot be resolved. It is indirectly referenced
         from required .class files

    Solution:

    This is because the reader's project does not exist. The portlet package causes the download of the portlet package.

    In addition to introducing the corresponding Portlet package, the reader can also

    import org.springframework.web.portlet.ModelAndView;import org.springframework.web.portlet.mvc.AbstractController;

    Changed:

    import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;
    4. Error 4:

    If the error is caused by the JSTL tag, download a tag standard document library and download the page.

    References
    • SpringMVC + MySQL instances
    • SpringMVC
    • Http://stackoverflow.com/questions/29473972/errorthe-type-javax-portlet-portletcontext-cannot-be-resolved

     

    This blog post is an original article by the blogger. If you need to repost it, please indicate the source.

    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.