Achieve SpringMvc with annotations

On the basis of the first complete spirngmvc code on:

Start-time code

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<% - at the time href = "some" of the project is to publish a directory to find: Visit web site is HTTP: // localhost / SpringMVC / some 
href = "/ some" directly to the server is looking for: access site at HTTP: / / localhost / some -%>
    <a href="some.do">请求</a>

</body>
</html>

web.xml

<! - Central scheduler ->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <! - write the servlet ->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <- mapping servlet path:! Jsp is through this path after the request, and then through servlet-class springmvc find who is ->
    <! - write "/" will put all the static requests to the central dispatcher, so if ggg.html will give handler, errors will occur can not find the 404 is not recommended ->
    <! - If you write " / * ", then all the requests will have to the central dispatcher, including dynamic index.jsp, so you can not use ->
    ! <- a * .do or * .go can solve this problem: 1 Let the path behind the plus .do submit requests such as: <a href = "some.do">.
    2. You can also write on the time of registration "/ request path .do"
    3.<url-pattern>*.do</url-pattern>
    .Do suffix i.e. all requests are received may be a central scheduler, without would not received ->
    <!---->
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

springmvc.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<- Register Processor:! Bean's id must be "/" at the beginning, because id is a path ->
<!--<bean id="/some.do" class="com.abc.handler.SomeHandler"/>-->
<-! Registered components scanner ->
    <context:component-scan base-package="com.abc.handler"/>
<! - begin setting up only a registration component scanner, and add a comment on the handler @Controller, click Request error 404 runs. Because requests to the central dispatcher, based on the central dispatcher servlet / XML by some, to find SomeHandler,
But now, although Scan to Base - Package Penalty for = "com.abc.handler" , but you configure specific processor name SomeHandler (you do not have to tell the system), as well as in the name of the handler method in (a processor may have several names),

-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

SomeHandler.java

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
/*import org.springframework.web.servlet.mvc.Controller;*/
/*

* @Service:service
* @Controller: Processor
* @Respostory: dao
* And @Component: same function, different meaning
*
* * / 
// 0. 
/ * 1. notes the need to implement the Controller interface
 2. When the interface with the imported package deleted, replaced annotation packet
 3. Unused interfaces of, handleRequest without rewriting method, the method name free to write, type of return value unchanged
 4.
 */

@Controller
public  class SomeHandler {

    public ModelAndView doFirst(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        Mv ModelAndView = new new ModelAndView ();
         // setViewName: What is the name in response to a view, the name should be written with respect to the name (when published to the server root directory of the project) under the webapp path 
        mv.setViewName ( "is available for purchase" );
        mv.addObject("message","helloSpringMvc");
        return mv;
    }
}

Only a start setting register scanner assembly, and in the handler add a comment @Controller, click Request error 404 runs. Because requests to the central dispatcher, based on the central dispatcher servlet / XML through some, find SomeHandler,
but now while scanning the base- Package = "com.abc.handler" , but the name of your specific processor configuration SomeHandler ( you do not have to tell the system), as well as the name of the method within the processor (can have several names within a processor),

I then add back in SomeHandler @Controller processor ( "/some.do"), as follows

@Controller("/some.do")
public class SomeHandler {...

The results given:

HTTP Status 500 – Internal Server Error


Type  Exception Report

消息 No adapter for handler [com.abc.handler.SomeHandler@3bdc7430]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

Central scheduler schedules when the need to include a processor adapter supports this. This time is because the method doFirst adapter can not find SomeHandler.java class (), this time the adapter can not fit this handler, because there is no correct way to respond to the

This time should be given path method needs to be configured in the way:

@RequestMapping ( "/ some.do") Request Mapping (/some.do can be mapped to the route request doFirst method): If it is time to find the /some.do a method doFirst

Code is modified as follows:

@Controller
 public  class SomeHandler {
     // map the request: If you are /some.do time to find a method doFirst 
@ RequestMapping ( "/ some.do" )
     public ModelAndView doFirst (javax.servlet.http.HttpServletRequest the HttpServletRequest, the javax.servlet HttpServletResponse .http.HttpServletResponse) throws Exception {
        Mv ModelAndView = new new ModelAndView ();
         // setViewName: What is the name in response to a view, the name should be written with respect to the name (when published to the server root directory of the project) under the webapp path 
        mv.setViewName ( "is available for purchase" );
        mv.addObject("message","helloSpringMvc");
        return mv;
    }
}
View Code

Click successfully interviewed.

(Why not add notes when you can find the corresponding way to do that, because at that time the code is as follows:

public class SomeHandler implements Controller{

    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        Mv ModelAndView = new new ModelAndView ();
         // setViewName: What is the name in response to a view, the name should be written with respect to the name (when published to the server root directory of the project) under the webapp path 
        mv.setViewName ( "is available for purchase" );
        mv.addObject("message","helloSpringMvc");
        return mv;
    }
}

 

SomeHandler implements the Controller interface Servlet class, and with a particular method name handleRequest, and now both are gone).

 

 

Then appeared the next question, if in the previous two handler methods add a comment @RequestMapping ( "/ some.do"), then the central dispatcher will find two different objects in the handler in accordance with /some.do method two processors,

It is given: to {/some.do}: There is already 'someHandler' bean method, add a solution namespace code:

@Controller
@RequestMapping ( "/ sss" )
 public  class xxxHandler {
     // At this point some of this work in this namespace sss the 
    @RequestMapping ( "/ some.do" )
     public ModelAndView AAA (the javax.servlet.http.HttpServletRequest HttpServletRequest, the javax HttpServletResponse .servlet.http.HttpServletResponse) throws Exception {

        return null;
    }
}
View Code
@Controller
@ RequestMapping ( "/ some") // namespace 
public  class SomeHandler {
     // map the request: If you are /some.do time to find a method doFirst
     // At this point in the provisions of this some / some namespace work , 
@RequestMapping ( "/ some.do" )
     public ModelAndView doFirst (the javax.servlet.http.HttpServletRequest HttpServletRequest, HttpServletResponse javax.servlet.http.HttpServletResponse) throws Exception {
        Mv ModelAndView = new new ModelAndView ();
         // setViewName: What is the name in response to a view, the name should be written with respect to the name (when published to the server root directory of the project) under the webapp path 
        mv.setViewName ( "is available for purchase" );
        mv.addObject("message","helloSpringMvc");
        return mv;
    }
}
View Code

Handler class to add a limitation, since a namespace, the method in order to avoid repetition path.

Further index.jsp should modify the path in the request: <a href="${pageContext.request.contextPath}/some/some.do"> request </a>, 404 will be without error.

 

Now it needs to change: now is /some.do can access dofirst () method, I would like to send a request fistdo also use dofirst () method in response to it by the.

In fact @RequestMapping ( "/ some.do"), a method in path value () is a string [] type, it can be written as implemented @RequestMapping ( "/ some.do", "fistdo");

 

Guess you like

Origin www.cnblogs.com/hyjh/p/11902332.html