springmvc view and view resolver

springmvc view and view resolver

Process model data

ModelAndView

Contains both view information and model data information.

Map and Model

All data in the model can be accessed through this input object, and new attribute data can also be added to the model.

MVC 's Handler acceptable method of ServletAPI types of parameters

HttpServletRequest

HttpServletResponse

HttpSession

java.security.Principal

Locale

InputStream

OutputStream

Reader

Writer

View resolver

After the request processing method is executed, it will finally return a ModelAndView object. ModelAndView contains the logical name and the view of the model object.

For processing methods that return String, View or ModeMap, Spring MVC will also assemble them into a ModelAndView object internally.

The view object is instantiated by the view resolver. Since views are stateless, they will not have thread safety issues

Common view resolvers

The programmer can choose one view resolver or mix multiple view resolvers.

Each view resolver implements the Ordered interface and exposes an order attribute. The order attribute can be used to specify the priority of the resolver. The smaller the order, the higher the priority.

InternalResourceViewResolver

If JSTL is used in the project, SpringMVC will automatically convert the view from InternalResourceView to JstlView.

If you use the fmt tag of JSTL, you need to configure the internationalized resource file in the SpringMVC configuration file.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/views/"></property>

    <property name="suffix" value=".jsp"></property>

</bean>

If you want to directly respond to the page rendered by SpringMVC, you can use the mvc:view-controller tag.

<mvc:view-controller path="/testMvcViewController" view-name="success" />

Redirect

If the returned string is prefixed with forward: or redirect:, Spring MVC will treat them specially: forward: and redirect: are used as indicators, and the following string is treated as a URL.

 

Static resources

The configuration of <mvc:default-servlet-handler/> in the SpringMVC configuration file solves the problem of static resources.

<mvc:default-servlet-handler/> will define a DefaultServletHttpRequestHandler in the context of SpringMVC, which will screen the request that enters the DispatcherServlet, and if it finds that it is a request that has not been mapped, the request will be handed over to the WEB application server by default Servlet processing, if it is not a static resource request, the DispatcherServlet will continue processing

view

Common view

You can configure one or more resolution strategies, and specify the order between them. Each mapping strategy corresponds to a specific

The view resolver implementation class.

Custom view

@Component("helloView1")

public class HelloView implements View{


@Override

public String getContentType() {

    return "text/html";

}


@Override

public void render(Map<String, ?> model, HttpServletRequest request,

HttpServletResponse response) throws Exception {

    response.getWriter().print("hello view, time: " + new Date());

}

}
@RequestMapping("/testView")

public String testView(User user){

System.out.println("testView");

return "helloView1";

}

 

Guess you like

Origin blog.csdn.net/weixin_44416039/article/details/86320396