2. SpringMVC non-annotated processor mappers and adapters

Keywords: Programming Java JSON JSP Database

Non-annotated processor mapper

<!-- To configure Handler -->
<bean id="itemsController" name="/queryItems.action" class="com.iot.ssm.controller.ItemsController"/>
<!-- Processor Mapper
//Finding the name of a bean as a URL requires specifying the bean name (that is, the url) when configuring the Handler
 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- simple url mapping-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <!-- Yes itemsController Conduct url mapping-->
            <prop key="/queryItems1.action">itemsController</prop>
            <prop key="/queryItems2.action">itemsController</prop>
        </props>
    </property>
</bean>

Multiple mappers can coexist, and the front-end controller decides which mappers a url can handle to get the right mapper to handle

Non-annotated processor adapter

 <!--Processor adapters, all of which implement the HandlerAdapter interface-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

Requires a Handler written to implement the Controller interface.

<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

Require Handler to implement HttpRequestHandler interface

import com.iot.ssm.po.Items;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ItemsController2 implements HttpRequestHandler{
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        //Call service to find database, query commodity list, use static data simulation here
        List<Items> itemsList = new ArrayList<Items>();

        //Filling a list with static data
        Items items_1 = new Items();
        items_1.setName("Lenovo notebook");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 Lenovo Laptop!");

        Items items_2 = new Items();
        items_2.setName("IPhone");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6 IPhone!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //Setting up model data
        httpServletRequest.setAttribute("itemsList",itemsList);

        //Set Forwarded View
        httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(httpServletRequest,httpServletResponse);
    }
}

The handleRequest method of the HttpRequestHandler adapter returns void, but does not return ModelAndView. You can modify the response content through response, such as returning json data:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json strand");

Posted by kornlord283 on Thu, 07 Nov 2019 13:42:06 -0800