springmvc----自定义视图示例

首先自定义视图类
实现View接口

package top.demo.springmvc.view;

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;

import com.mysql.fabric.xmlrpc.base.Data;


@Component
public class MyView implements View{

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

	@Override
	public void render(Map arg0, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.getWriter().append("Hi this is MyView Render text!");
		response.getWriter().append("
"); } }

springmvc配置文件




	
	

	

	
		
		
	
	
	
	
	
	
	
	
	
	
		
	



配置文件里还有其他的测试 这里例子主要内容是

	
		
	

BeanNameViewResolver该视图解析器会从容器中找符合名字的bean当做视图,
其次,order属性定义了视图解析器的优先级,InternalResourceViewResolver的默认优先级是int的最大值。我们需要让自定义的视图和其解析器优先级在其前面,所有定义了order

控制器

package top.demo.springmvc.handler;

import java.util.Map;
import java.util.Properties;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;


import top.demo.test.pojo.Addr;
import top.demo.test.pojo.Man;

@Controller
public class HelloWorld {
	
	public HelloWorld() {
		
		System.out.println("HelloWorld construct.....");
	}
	
	
	@RequestMapping("/helloword")
	public String hi() {
		
		System.out.println("HelloWorld");
		return "success";
		
	}
	
	@RequestMapping("/testPojo")
	public String testPojo(Man man) {
		
		System.out.println(man);
		
		 Properties initProp = new Properties(System.getProperties());
		 System.out.println("当前系统编码:" + initProp.getProperty("file.encoding"));
		 System.out.println("当前系统语言:" + initProp.getProperty("user.language"));

		return "success";
	}
	
	@ModelAttribute
	public void getMan(@RequestParam(name="id",required=false) Integer id,Map map) {
		
		/*
		 * 模拟从数据库先取出id所对应的条目
		 * @ModelAttribute在每一个@RequestMapping方法前都会运行
		 * 放入map的key  必须 和@RequestMapping方法的形参的类型名 第一个字母小写一致 
		 * 否则 @RequestMapping方法获取不到map里的对象
		 * */
		System.out.println("@ModelAttribute getMan"+id);
		if(id!=null) {
			
			Man man=new Man(1,"testName",18,new Addr("中国","zg"));
			System.out.println("@ModelAttribute getMan"+man);
			map.put("man", man);
		}
		
	}
	
	@RequestMapping("/testModelAttr")
	public String testModelAttr(Man man) {
		
		System.out.println("testModelAttr"+man);
		return "success";
	}
	
	
	@RequestMapping("/testview")
	public String testMyView() {
		
		System.out.println("test view control");
		
		return "myView";
	}

}

主要是这里

	@RequestMapping("/testview")
	public String testMyView() {
		
		System.out.println("test view control");
		
		return "myView";
	}

返回了视图的名字 为myView 所以BeanNameViewResolver会从容器中找符合名字的bean,而在定义MyView时已经用@Component将其加入容器。
所以整个流程,我这个例子,访问/testview会转到MyView视图

springmvc----自定义视图示例_第1张图片

你可能感兴趣的:(JavaEE)