Since the implementation of custom views

Since it is a self-defined view of the implementation process, we have to customize a view;

Here it is a custom view MyView as an example:

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;

@Component
public class MyView implements View {

	public String getContentType() {
		return "text/html";
	}

	public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PrintWriter out = response.getWriter();
		out.println("Hello View");
		out.flush();
		out.close();
	}
}

Then application.xml configuration, put it in a container IOC;

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
	p:prefix="/WEB-INF/view/" p:suffix=".jsp"></bean>

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"></bean>

In view name and then return to the Controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserInfoController {

	@RequestMapping("userinfo/toView.do")
	public String toView(Map<String,Object> map) {
		map.put("name", "Tom");
		return "myView";
	}
}

Web access to the following figure:

         

Then we look at the source code (Debug mode):

According to my previous blog: a * .do execution of the request  shows: process the request will be executed doDispatch method, so this time we started watching from doDispatch method:

When the first red box to execute the code of access to information mv, after the execution of our mouse on the mv mv information at this time will show:

We define the name from the view (the default custom view class from the class name first letter lowercase) and model data has been acquired in the mv; then execute the second red box code execution processDispatchResult method code is as follows:

That part of the red box if statement determines whether the program is abnormal, there is no abnormality, the red line is then performed render method, which code is as follows:

Red box to perform a first code, we need to get the view objects; resolveViewName thus performed, the method code is as follows:

Traversing view resolution configured class; classes you need to find the view resolution;

Mouse over  this.viewResolvers  we find our two views parsing class configuration; wherein the first view is custom parser class; then execute  resolveViewName method; i.e., the second red line on FIG resolveViewName method, the method inherits in BeanNameViewResolver class (class parsing custom view), as follows:

该方法将返回获取的视图对象;于是便回到上图 render 方法的第二个红框处,我们可以看到,此时的 view 对象已是我们的自定义视图对象:

然后执行我们自定义视图的 render 方法,即我们写的方法,输出结果:

 

发布了99 篇原创文章 · 获赞 3 · 访问量 1203

Guess you like

Origin blog.csdn.net/qq_44971038/article/details/104556163