一个login.html,一个demo.html,两个页面都放在resource/templates底下,登录界面使用modalAndView的方式实现,可以访问,然后点击登录界面的登录按钮 写一个js:window.location.href=“demo.html”;报404,请问是路径错了?应该怎么写路径?
你用得什么模板引擎啊?resource/templates
路径下的是模板引擎,不能通过浏览器直接访问。要通过Controller
的ModelAndView
跳转。或者设置ViewControllers
。
但是使用ModelAndView,前段界面应该怎么调用呢?现在我使用了ModelAndView跳转到demo.html,但是浏览器还停留在login.html,后端:
@RequestMapping("/index")
public ModelAndView index(){
ModelAndView model = new ModelAndView();
model.setViewName("test.html");
//return ResultUtil.success("操作成功", "", 0);
return model;
}
前端调用:
<script type="text/javascript">
function goindex() {
$.ajax({
url: "http://localhost:8888/test/index",
data: {},
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(data) {
/*if(data.code==0){
//alert("错误信息:"+data.msg);
//location.reload();
}else{
//var curWwwPath = window.document.location.href;
alert(data.msg);
window.location.href="test.html";
}*/
alert("跳转成功");
}
})
}
</script>
再写一个Controller
@RequestMapping("/login")
public ModelAndView index(){
ModelAndView model = new ModelAndView();
model.setViewName("login.html");
return model;
}
前端跳转
window.location.href = 'login';
login我写了的,我帖一下所有代码吧:
后端:
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private ItfSysUserService ItfSysUserService;
@RequestMapping("/login")
public String login(){
// String path = System.getProperty("user.dir");
//System.out.println("=============:"+path);
return "login";
}
@RequestMapping("/index")
public ModelAndView index(){
ModelAndView model = new ModelAndView();
model.setViewName("test.html");
//return ResultUtil.success("操作成功", "", 0);
return model;
}
前端:
<script type="text/javascript">
function goindex() {
$.ajax({
url: "http://localhost:8888/test/index",
data: {},
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(data) {
/*if(data.code==0){
//alert("错误信息:"+data.msg);
//location.reload();
}else{
//var curWwwPath = window.document.location.href;
alert(data.msg);
window.location.href="test.html";
}*/
alert("跳转成功");
}
})
}
</script>
html调用的地方:
<tr>
<td>
<input type="button" value="登录" id="login" onclick="goindex()" />
<input type="button" value="重置" id="reset" onclick=""/>
</td>
</tr>
你index
都加了.html
,这里为啥不加了。