SPRINGMVC Study Notes

Source: Internet
Author: User

First, SPRINGMVC environment construction

1. Create a new file and select Java→pack Explore view

2, the Requai under the Libs under the jar package into the

3. Web. XML start Configuration Springmvc

<servlet>      <Servlet-name>Springmvc</Servlet-name>      <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>"It's supposed to be under the Spring-webmvc-3.2.0.release.jar under this jar.org.springframework.web.servlet root directory】
<Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath*:config/spring-servlet.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping>

4. Configuring the SRIMGMVC configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"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-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/con Text/spring-context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd ">                  <Beanname= "/test1/helloworld"class= "Com.tgb.web.controller.HelloWorldController" />        <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value="/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean> </Beans>  

The above class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"

Under the Spring-webmvc-3.2.0.release.jar.

Org.springframework.web.servlet.view under the

Parser for Internalresourceviewresolver.class View decomposition

Second, section II

1, create a new controller.

 PackageCom.tgb.web.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller; Public classHelloworldcontrollerImplementscontroller{@Override PublicModelandview handlerequest (httpservletrequest arg0, httpservletresponse arg1)throwsException {System.out.println ("HelloWorld, video of the first Springmvc"); return NewModelandview ("/welcome"); }}

How does spring find this controller?

In spring, the configuration file Spring-servlet.xml "defaults to its path and to the Web. XML sibling. Web.xml→spring-servlet.xml "

Could not find the resource file Spring-servlet.xml "solution: Spring-servlet.xml modified to Springmvc-servlet.xml. Modified to the default filename."

Fill in <bean name= "/test1/helloworld" class= "Com.tgb.web.controller.HelloWorldController"/>

Finally: Browser input http://localhost:8080/springMVC1/test1/helloworld can be accessed

Iii. modifying configuration files and passing parameters to the interface

1. The configuration file is not in the default path

Create a new config directory under SRC,

View the source code for the Distributor. Can know how to find the directory. To Contextconfiglocation

Modifying the initialization parameters of Web. XML SPRINGMVC

    < Init-param >              < Param-name >contextconfiglocation</param-name>              <  Param-value>classpath*:config/spring-servlet.xml</param-value >          </ Init-param >

2. Passing parameters

Inside control//return string number and//the following is the return map data


String Hello = "lsh Hello accelerated";
map<string,object> map = new hashmap<string,object> ();
Map.put ("Map1", "Accelerated 1");
Map.put ("Map2", "Accelerated 2");
Map.put ("Map3", "Accelerated 3");
Returns a String data
return new Modelandview ("/welcome", "result", "hello");
Here is the return map data
return new Modelandview ("/welcome", "map", map);

To use the ESTL expression, introduce the following two packages.

Jstl's jar Package

standard-1.1.2 's jar Package

Iv. SPRINGMVC implement a controller to write multiple methods

Create a new controller

1, public class Multicontroller inherit a Multiactioncontroller

Package Com.tgb.web.controller;Import Javax.servlet.http.HttpServletRequest;Import Javax.servlet.http.HttpServletResponse;Import Org.springframework.web.servlet.ModelAndView;Import Org.springframework.web.servlet.mvc.multiaction.MultiActionController;Public class Multicontroller extends Multiactioncontroller {public Modelandview Add (httpservletrequest request,httpservletresponse Response) {System.out.println ("----Add method OK----");return new Modelandview ("/multi", "Method", "add");} public Modelandview update (HttpServletRequest request,httpservletresponse Response) {System.out.println ("-----Update---method");return new Modelandview ("/multi", "Method", "Update");    }            }

2, SPRINGMVC configuration inside how to write, is under the Springmvc-servlet.xml

 <  bean  id  = "Parammethodresolver"   class  = "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver " >  <  property  name  = "ParamName"  Span style= "color: #ff0000;" > value  = "action"  ></ property  >  </ bean  >  

Under the Spring WEBMVC Rack package under MVC multiaction. Parametermethodnameresolver

Value= "Action"> when requested, the parameter also changes with this.

A parser for the request token

Continue to configure

 <Beanname= "/test1/multi"class= "Com.tgb.web.controller.MultiController">        < Propertyname= "Methodnameresolver">            <refBean= "Parammethodresolver"/>        </ Property>    </Bean>    

Then browser input: Http://localhost:8080/springMVC1/test1/multi?action=add

Not requested, solution public Modelandview Add (httpservletrequest request,httpservletresponse response) requires two parameters in parentheses

Section V, 5th, access to static files

1. Create a new controller

 PackageCom.tgb.web.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classStaticcontrollerextendsMultiactioncontroller { Publicmodelandview img (httpservletrequest request,httpservletresponse response) {return NewModelandview ("/staticfile"); }         Publicmodelandview jquery (httpservletrequest request,httpservletresponse response) {return NewModelandview ("/jquery"); }    }

2. Create a new JSP

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><% @taglib prefix=" C "uri=" http://java.sun.com/jsp/jstl/ Core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

<Beanname= "/test1/img"class= "Com.tgb.web.controller.StaticController">        < Propertyname= "Methodnameresolver">            <refBean= "Parammethodresolver"/>        </ Property>    </Bean>    

The address is correct, the picture cannot be taken, the static file cannot get

Continue to write an MVC tag in the SPRINGMVC configuration file.

Do not intercept, direct access

<!--static resource access-     <mvc:resources location= "/img/" mapping= "/img/**"/> <mvc:resources location=       "/js/" mapping= "/js/**"/>       

SPRINGMVC Study Notes

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.