首页 新闻 会员 周边

SpringBoot 利用HTML实现页面跳转(SSM框架+Mysql)

0
悬赏园豆:20 [已解决问题] 解决于 2018-11-06 17:14

Controller:
// 实现登录验证
@RequestMapping("userLogin")
public String userLogin(Model model,HttpServletRequest request, HttpServletResponse response) {
String name=request.getParameter("name");
String password=request.getParameter("password");
User user = bookService.selectByKey(name, password);
System.out.println(user);

    if (user != null) {
        request.getSession().setAttribute("session_user", user); // 将用户信息放入session
        model.addAttribute("name",name);
        model.addAttribute("password", password);
        return "main.html";
    }
    return "login.html";
}

main.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>图书管理系统</title>
</head>
<body>
<h1>登录成功</h1>
<p th:text="${user}">user</p>

</body>
</html>
返回的结果:
只显示main.html字符串,没有页面跳转。怎么修改呀?各位大神们。

Programer-Girl的主页 Programer-Girl | 初学一级 | 园豆:173
提问于:2018-11-06 16:39
< >
分享
最佳答案
1

将 .html 去掉,controller层 需加上@controller

收获园豆:20
番茄先生 | 小虾三级 |园豆:911 | 2018-11-06 16:50

controller层我加的@RestController,.html去掉也是返回字符串。

Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 17:03

@Programer-Girl: @RestController内部直接含有@responsebody,你换成@controller就好了,需要用到返回数据得就在方法体上加上@responsebody,用modeandview形式返回数据和跳转页面

番茄先生 | 园豆:911 (小虾三级) | 2018-11-06 17:05

@番茄先生: 真的太感谢啦,问题解决了,嘻嘻嘻,笔芯。

Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 17:13

@Programer-Girl: springboot这个框架还是可以的,配置简化,容易理解,好好加油!!!

番茄先生 | 园豆:911 (小虾三级) | 2018-11-06 17:17
其他回答(1)
0

你前端传输数据怎么写的,你贴一下

PottyHarry | 园豆:302 (菜鸟二级) | 2018-11-06 16:48

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>用户登录</title>
</head>
<body>
图书馆里系统
<form th:action="@{/userLogin}" method="post">
用户名:<input type="text" name="name" /><br></br>
密码:<input type="password" name="password" /><br></br>
<input type="submit" value="登录" />
</form>
</body>
</html>

支持(0) 反对(0) Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 16:49

@Programer-Girl:大体了解,你是用form表单提交的方法,因为SSM框架其实封装了不少请求流程方面的问题,因为你参数里面也有关于request和response对象的获取,那好,你先试下以下两种方法
转发:
request.getRequestDispatcher("").forward(request, response);
重定向:
response.sendRedirect("");
不用返回值的形式,因为返回值的形式就是框架通过拼接你的字符串进行页面跳转,如果跳转页面不成功,会使用response.getWriter().write(字符串)的形式输出页面,这也是你跳转页面出现你返回值的问题所在,所以你试下转发和重定向,看下是否能够实现正常页面跳转。

支持(0) 反对(0) PottyHarry | 园豆:302 (菜鸟二级) | 2018-11-06 16:54

package com.sun.book;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sun.book.dao.BookService;
import com.sun.book.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

@RestController //@Controller+@ResponseBody
@RequestMapping("/")
public class BookController {
@Autowired
BookService bookService;

// 先跳转到登陆画面
@RequestMapping("/loginhtml")
public ModelAndView loginhtml(ModelAndView mav,Model model) {
    mav.setViewName("login");
    
    return mav;
}

// 实现登录验证
@RequestMapping("userLogin")
public String userLogin(Model model,ModelAndView mav,HttpServletRequest request, HttpServletResponse response) {
    String name=request.getParameter("name");
    String password=request.getParameter("password");
    User user = bookService.selectByKey(name, password);
    System.out.println(user);
    
    if (user != null) {
        request.getSession().setAttribute("session_user", user); // 将用户信息放入session
        model.addAttribute("user",user);
        return "/main";
    }
    return "/login";
}

}
这是全部controller。user实体数据能取到,只能用ModelAndView才能跳转。
网上好多用以String为返回值类型的例子都能跳转。我返回的就是字符串。

支持(0) 反对(0) Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 16:56

@Programer-Girl: 为啥返回字符串,返回字符串就是你有用到@responsebody注解,你方法体上没用是不是你controller用得@restcontroller

支持(0) 反对(0) 番茄先生 | 园豆:911 (小虾三级) | 2018-11-06 16:59

@PottyHarry: 你好,跳转可以成功,但是如果我想用返回值为String的形式,要怎么实现呀?

支持(0) 反对(0) Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 17:02

@Programer-Girl: 哇,看到你控制类前的注解了,@RestController //@Controller+@ResponseBody
@ResponseBody的作用类似于response.getWriter().write(),也就是你控制器里对应页面的处理方法,无论返回什么,都会原原本本输出到前端页面,而我前面让你进行转发或者重定向,就是跳过了这个步骤,自己实现跳转,如果你经常写ajax,肯定会深有体会,因为ajax获取后台返回值的时候,方法前面是要注释@ResponseBody的,所以你去掉类前的这个注解再试试,还有你项目对于页面的前后缀有没有自己定义配置,因为spring-mvc中有个视图是配置了前缀和后缀的,这个时候你只要输入页面名就可以了,输全还会出现404的错误。

支持(0) 反对(0) PottyHarry | 园豆:302 (菜鸟二级) | 2018-11-06 17:12

@PottyHarry:我已经结贴了,还是非常感谢你的答复。豆子可能给不了你了,非常抱歉。

支持(0) 反对(0) Programer-Girl | 园豆:173 (初学一级) | 2018-11-06 17:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册