文章目录
行创云篇
一 、后端控制的页面跳转
-
第一种跳转到某个视图层,如tiles文件中定义的
login
,指向页面的/WEB-INF/views/account/login.jsp
所以当我使用return new ModelAndView("login");
时实际是找到tiles中名为login
的视图,渲染对应的login.jsp
页面到返回给前端显示。[渲染视图和页面数据]tiles.xml部分代码:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd"> <tiles-definitions> <!-- account settings --> <definition name="login" extends="simpleLayout"> <put-attribute name="content" value="/WEB-INF/views/account/login.jsp"/> </definition> <definition name="regist" extends="simpleLayout"> <put-attribute name="content" value="/WEB-INF/views/account/regist.jsp"/> </definition> ··· <!--公司主页--> <definition name="trace-home" extends="singleLayout"> <put-attribute name="content" value="/WEB-INF/views/trace/home.jsp"/> </definition> <!--项目主页--> <definition name="project-index" extends="ns-layout"> <put-attribute name="content" value="/WEB-INF/views/trace/project-index.jsp"/> </definition> <definition name="trace-index" extends="simpleLayout"> <put-attribute name="content" value="/WEB-INF/views/trace/index.jsp"/> </definition> </tiles-definitions>
-
第二种,拼接地址跳转,相当于戳后台指定接口。如CommonTraceController.java中有如下模块,当我在控制层执行如下代码时
return new ModelAndView(new RedirectView("/trace", true));
时,会去控制层找到xx/trace接口,按CommonTraceController.java中声明的接口实际返回return new ModelAndView("project-index")
,这个时候同第一种请求方式,即在tiles.xml中找到标签为“project-index”的页面/WEB-INF/views/trace/project-index.jsp,渲染对应jsp的视图和数据注:public RedirectView(String url, boolean contextRelative)函数表示重定向的一种构造方式,url表示需要重定向的地址,contextReleative表示是否延续之前的请求地址。如在localhost:8080/login下请求了该控制层,当contextReleative=true的会重定向到localhost:8080/trace;反之false会重定向到localhost:8080/login/trace
CommonTraceController.java部分代码
@Controller @RequestMapping("/trace") public class CommonTraceController extends BaseController{ @RequestMapping(value={"/index"}, method = RequestMethod.GET) public String index(Model model) { return "trace-index"; } @RequestMapping(value={"/home"}, method = RequestMethod.GET) public String home(Model model) { return "trace-home"; } @RequestMapping(value={""}, method = RequestMethod.GET) public String projectIndex(Model model) { return "project-index"; } ··· // 选择合同公用页面 @RequestMapping(value={"/public/select/contract"}, method = RequestMethod.GET) public String publicSelectContract(Model model) { return "trace-public-select-contract"; } // 选择材料公用页面 @RequestMapping(value={"/public/select/material"}, method = RequestMethod.GET) public String publicSelectMaterial(Model model) { return "trace-public-select-material"; } // 选择材料类别公用页面 @RequestMapping(value={"/public/select/material/category"}, method = RequestMethod.GET) public String publicSelectMaterialCategory(Model model) { return "trace-public-select-material-category"; }
}
```
参考资料