类org.springframework.web.servlet.view.DummyMacroRequestContext源码实例Demo

下面列出了怎么用org.springframework.web.servlet.view.DummyMacroRequestContext的API类实例代码及写法,或者点击链接到github查看源代码。

private String getMacroOutput(String name) throws Exception {
	String macro = fetchMacro(name);
	assertNotNull(macro);

	FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
	FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));

	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean darren = new TestBean("Darren", 99);
	TestBean fred = new TestBean("Fred");
	fred.setJedi(true);
	darren.setSpouse(fred);
	darren.setJedi(true);
	darren.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", darren);

	Map<String, String> names = new HashMap<>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");
	names.put("Rob&Harrop", "Rob Harrop");

	Configuration config = fc.getConfiguration();
	Map<String, Object> model = new HashMap<>();
	model.put("command", darren);
	model.put("springMacroRequestContext", rc);
	model.put("msgArgs", new Object[] { "World" });
	model.put("nameOptionMap", names);
	model.put("options", names.values());

	FreeMarkerView view = new FreeMarkerView();
	view.setBeanName("myView");
	view.setUrl("tmp.ftl");
	view.setExposeSpringMacroHelpers(false);
	view.setConfiguration(config);
	view.setServletContext(new MockServletContext());

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	output = output.replace("\r\n", "\n");
	return output.trim();
}
 
private String getMacroOutput(String name) throws Exception {
	String macro = fetchMacro(name);
	assertNotNull(macro);

	FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
	FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));

	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean darren = new TestBean("Darren", 99);
	TestBean fred = new TestBean("Fred");
	fred.setJedi(true);
	darren.setSpouse(fred);
	darren.setJedi(true);
	darren.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", darren);

	Map<String, String> names = new HashMap<>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");
	names.put("Rob&Harrop", "Rob Harrop");

	Configuration config = fc.getConfiguration();
	Map<String, Object> model = new HashMap<>();
	model.put("command", darren);
	model.put("springMacroRequestContext", rc);
	model.put("msgArgs", new Object[] { "World" });
	model.put("nameOptionMap", names);
	model.put("options", names.values());

	FreeMarkerView view = new FreeMarkerView();
	view.setBeanName("myView");
	view.setUrl("tmp.ftl");
	view.setExposeSpringMacroHelpers(false);
	view.setConfiguration(config);
	view.setServletContext(new MockServletContext());

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	output = output.replace("\r\n", "\n");
	return output.trim();
}
 
private String getMacroOutput(String name) throws Exception {
	String macro = fetchMacro(name);
	assertNotNull(macro);

	FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
	FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));

	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<String, String>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<String, String>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean darren = new TestBean("Darren", 99);
	TestBean fred = new TestBean("Fred");
	fred.setJedi(true);
	darren.setSpouse(fred);
	darren.setJedi(true);
	darren.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", darren);

	Map<String, String> names = new HashMap<String, String>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");
	names.put("Rob&Harrop", "Rob Harrop");

	Configuration config = fc.getConfiguration();
	Map<String, Object> model = new HashMap<String, Object>();
	model.put("command", darren);
	model.put("springMacroRequestContext", rc);
	model.put("msgArgs", new Object[] { "World" });
	model.put("nameOptionMap", names);
	model.put("options", names.values());

	FreeMarkerView view = new FreeMarkerView();
	view.setBeanName("myView");
	view.setUrl("tmp.ftl");
	view.setExposeSpringMacroHelpers(false);
	view.setConfiguration(config);
	view.setServletContext(new MockServletContext());

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	return output.trim();
}
 
@Test
public void allMacros() throws Exception {
	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<String, String>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<String, String>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean tb = new TestBean("Darren", 99);
	tb.setJedi(true);
	tb.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", tb);

	Map<String, String> names = new HashMap<String, String>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");

	VelocityConfigurer vc = new VelocityConfigurer();
	vc.setPreferFileSystemAccess(false);
	VelocityEngine ve = vc.createVelocityEngine();

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("command", tb);
	model.put("springMacroRequestContext", rc);
	model.put("nameOptionMap", names);

	VelocityView view = new VelocityView();
	view.setBeanName("myView");
	view.setUrl("org/springframework/web/servlet/view/velocity/test.vm");
	view.setEncoding("UTF-8");
	view.setExposeSpringMacroHelpers(false);
	view.setVelocityEngine(ve);

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	System.out.println(output);
	String[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");

	for (int i = 0; i < tokens.length; i++) {
		if (tokens[i].equals("NAME")) assertEquals("Darren", tokens[i + 1]);
		if (tokens[i].equals("AGE")) assertEquals("99", tokens[i + 1]);
		if (tokens[i].equals("MESSAGE")) assertEquals("Howdy Mundo", tokens[i + 1]);
		if (tokens[i].equals("DEFAULTMESSAGE")) assertEquals("hi planet", tokens[i + 1]);
		if (tokens[i].equals("THEME")) assertEquals("Howdy! Mundo!", tokens[i + 1]);
		if (tokens[i].equals("DEFAULTTHEME")) assertEquals("hi! planet!", tokens[i + 1]);
		if (tokens[i].equals("URL")) assertEquals("/springtest/aftercontext.html", tokens[i + 1]);
		if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM2")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" class=\"myCssClass\">", tokens[i + 1]);
		if (tokens[i].equals("FORM3")) assertEquals("<textarea id=\"name\" name=\"name\" >Darren</textarea>", tokens[i + 1]);
		if (tokens[i].equals("FORM4")) assertEquals("<textarea id=\"name\" name=\"name\" rows=10 cols=30>Darren</textarea>", tokens[i + 1]);
		//TODO verify remaining output (fix whitespace)
		if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
		if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" />", tokens[i + 2]);
		if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
		if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"checked\" />", tokens[i + 2]);
	}
}
 
@Test
public void idContainsBraces() throws Exception {
	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<String, String>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<String, String>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean darren = new TestBean("Darren", 99);
	TestBean fred = new TestBean("Fred");
	fred.setJedi(true);
	darren.setSpouse(fred);
	darren.setJedi(true);
	darren.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", darren);

	Map<String, String> names = new HashMap<String, String>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");

	VelocityConfigurer vc = new VelocityConfigurer();
	vc.setPreferFileSystemAccess(false);
	VelocityEngine ve = vc.createVelocityEngine();

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("command", darren);
	model.put("springMacroRequestContext", rc);
	model.put("nameOptionMap", names);

	VelocityView view = new VelocityView();
	view.setBeanName("myView");
	view.setUrl("org/springframework/web/servlet/view/velocity/test-spr5172.vm");
	view.setEncoding("UTF-8");
	view.setExposeSpringMacroHelpers(false);
	view.setVelocityEngine(ve);

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	String[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");

	for (int i = 0; i < tokens.length; i++) {
		if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", tokens[i + 1]); //
		if (tokens[i].equals("FORM2")) assertEquals("<textarea id=\"spouses0.name\" name=\"spouses[0].name\" >Fred</textarea>", tokens[i + 1]);
		if (tokens[i].equals("FORM3")) assertEquals("<select id=\"spouses0.name\" name=\"spouses[0].name\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM4")) assertEquals("<select multiple=\"multiple\" id=\"spouses\" name=\"spouses\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM5")) assertEquals("<input type=\"radio\" name=\"spouses[0].name\" value=\"Darren\"", tokens[i + 1]);
		if (tokens[i].equals("FORM6")) assertEquals("<input type=\"password\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM7")) assertEquals("<input type=\"hidden\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", tokens[i + 1]);
		if (tokens[i].equals("FORM8")) assertEquals("<input type=\"hidden\" name=\"_spouses0.name\" value=\"on\"/>", tokens[i + 1]);
		if (tokens[i].equals("FORM8")) assertEquals("<input type=\"checkbox\" id=\"spouses0.name\" name=\"spouses[0].name\" />", tokens[i + 2]);
		if (tokens[i].equals("FORM9")) assertEquals("<input type=\"hidden\" name=\"_spouses0.jedi\" value=\"on\"/>", tokens[i + 1]);
		if (tokens[i].equals("FORM9")) assertEquals("<input type=\"checkbox\" id=\"spouses0.jedi\" name=\"spouses[0].jedi\" checked=\"checked\" />", tokens[i + 2]);
	}
}
 
 同包方法