spring mvc2

web intercepts way

springconfig.xml

    <! - does not block static resources -> 
    <MVC: default -servlet-Handler />

/ Interception that do not contain jsp, including today's resources, css, js, images

 / * Block all (true interception)

web.xml

<!-- 配置SpringMVC前端控制器 -->
    <servlet>
        <servlet-name>spring_mvc_test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springconfig.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring_mvc_test</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

@RequestMapping

You can define different processor mapping rules @RequestMapping comment.

@RequestMapping ( "") effect

Marked on the method: uri access controller;

Marked on the category: functional difference is that, an additional layer of access control path before uri

URL path mapping

@RequestMapping (value = "item") or @RequestMapping ( "/ Item" ) 
value is an array value, may be mapped to the same method plurality url 
/ ** 
 * Query Product List 
 * @return 
 * / 
@RequestMapping (value {= "the itemList", "itemListAll" })
 public ModelAndView queryItemList () {
     // query product data 
    List <Item> List = the this .itemService.queryItemList (); 

    // Create ModelAndView, set the logical view name 
    ModelAndView = Music Videos new new ModelAndView ( "the itemList" ); 

    // put into the product data model 
    mv.addObject ( "the itemList" , List);
     return Music Videos; 
}

Add in the class above

Added to the class @RequestMapping (url) specify the generic prefix request, restrict all such methods the request url must begin with the prefix request, this method can be used to classify management url, as shown below

At this time, you need to enter queryItemList () method is a request url:

http://127.0.0.1:8080/springmvc-web2/item/itemList.action

or

http://127.0.0.1:8080/springmvc-web2/item/itemListAll.action

Request methods defined

The method of addition can be set to url, the incoming request may also be defined 
 GET method defined 
@RequestMapping (Method = RequestMethod.GET) 

If access via the POST error: 
the HTTP the Status 405 - the Request Method 'POST' Not Supported 

example: 
@RequestMapping ( value = "the itemList", method = RequestMethod.POST) 

 POST method defined 
@RequestMapping (method = RequestMethod.POST) 

if accessed by the GET error: 
the HTTP the Status 405 - the Request method 'GET' Not Supported 

 GET and POST are 
! @RequestMapping (Method = {RequestMethod.GET, RequestMethod.POST})

Controller method returns a value

Return ModelAndView

The method defined in ModelAndView controller and returns the object, the object may be added to model data, the specified view.

Return void

The method can be defined on the Controller parameter request and response, request or response using the specified response result:
 1 , using the page forward request, as follows: 
request.getRequestDispatcher ( "Page path" ) .forward (request, response); 
request.getRequestDispatcher ( " /WEB-INF/jsp/success.jsp " ) .forward (Request, response);

 2 , page redirection response by: 
Response.sendRedirect ( " URL " ) 
Response.sendRedirect ( " /springmvc-web2/itemEdit.action " );

 3 , can be specified response through a response result, for example in response json data was as follows: 
response.getWriter () Print (. " {\ "ABC \": 123} ");

 

Guess you like

Origin www.cnblogs.com/taozizainali/p/11223123.html