首先自定义视图类
实现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<String, ?> arg0, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().append("Hi this is MyView Render text!");
response.getWriter().append("<br>");
}
}
springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="top.demo.springmvc">
</context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 如果需要直接到视图 不经过控制器 可以使用 mvc:view-controller标签-->
<mvc:view-controller path="/success" view-name="success"/>
<!-- 通常,如果希望mvc:view-controller配置的view 能直接到视图又还能够经过控制器需要配置mvc:annotation-driven -->
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
</beans>
配置文件里还有其他的测试 这里例子主要内容是
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
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<String,Object> 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视图