【问题标题】:Spring boot not redirecting to desired pageSpring Boot 未重定向到所需页面
【发布时间】:2016-10-24 15:34:12
【问题描述】:

下面是我的控制器类。

@Controller
public class BankController {

@Autowired
private CustomerService customerService;
@Autowired
private CustomerRepository customerRepository;

@RequestMapping(value="/login", method=RequestMethod.POST)
public String actionLogin(@RequestParam String email, @RequestParam String password, Model model){
    List<Customer> byEmail = customerRepository.findByEmail(email);
    Customer customer = byEmail.get(0);
    if(BCrypt.checkpw(password, customer.getHashedPassword())){
        model.addAttribute("customer", customer);
        return "enterSiteView.html";
    }


    return "errorView.html";
}

我已将enterSiteView.html, errorView.html 保存在文档中给出的模板文件夹中。 当我单击登录按钮时,url 确实更改为http://localhost:9090/enterSiteView.html,但它显示错误。

Whitelabel Error Page 
application has no explicit mapping for /error, so you are seeing this as a     fallback.

Sun Oct 23 14:03:43 IST 2016
There was an unexpected error (type=Not Found, status=404).
No message available`

我的enterSiteView.html 中只有一条Hello World 文本。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Earth</title>
</head>
<body>

</body>
</html>

登录页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign-Up/Login Form</title>
<link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>

<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>

<body>

<div class="form">

<ul class="tab-group">
    <li class="tab active"><a href="#signup">Sign Up</a></li>
    <li class="tab"><a href="#login">Log In</a></li>
</ul>

<div class="tab-content">
    <div id="signup">
        <h1>Sign Up for Free</h1>

        <form action="/register" method="post">

            <div class="top-row">
                <div class="field-wrap">
                    <label>
                        First Name<span class="req">*</span>
                    </label>
                    <input type="text" required autocomplete="off"/>
                </div>

                <div class="field-wrap">
                    <label>
                        Last Name<span class="req">*</span>
                    </label>
                    <input type="text" required autocomplete="off"/>
                </div>
            </div>

            <div class="field-wrap">
                <label>
                    Email Address<span class="req">*</span>
                </label>
                <input type="email" required autocomplete="on"/>
            </div>

            <div class="field-wrap">
                <label>
                    Set A Password<span class="req">*</span>
                </label>
                <input type="password" required autocomplete="off"/>
            </div>

            <button type="submit" class="button button-block"/>Get Started</button>

        </form>

    </div>

    <div id="login">
        <h1>Welcome Back!</h1>

        <form action="/login" method="post">

            <div class="field-wrap">
                <label>
                    Email Address<span class="req">*</span>
                </label>
                <input type="email" id="email" name="email" required autocomplete="off"/>
            </div>

            <div class="field-wrap">
                <label>
                    Password<span class="req">*</span>
                </label>
                <input type="password" id="password" name="password" required autocomplete="off"/>
            </div>

            <p class="forgot"><a href="#">Forgot Password?</a></p>

            <button class="button button-block"/>
            Log In</button>

        </form>

    </div>

</div><!-- tab-content -->

</div> <!-- /form -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>

</body>
</html>

我在欢迎页面的静态中放置了 index.html。登录成功后,它应该重定向到所需的页面,而不是显示上述错误。

【问题讨论】:

    标签: html spring spring-boot


    【解决方案1】:

    我们有 java web 应用程序的公共路径scheme

    [host]:[port]/MyWebApplication/personal/info/top.html
    

    在哪里:

    • MyWebApplication 是context path(在微服务应用程序中映射 默认为root)
    • 个人 - 是servlet path
    • info/top.html 是path info

      requestURI = contextPath + servletPath + pathInfo

    所以你基于相对路径重定向,这意味着如果你:指定这种方式:

    return "redirect: errorView.html"
    

    它指引你

    (requestURI - last path part) + errorView.html 
    

    如果您来自 http://example.com/user/path/to/info.html ,则重定向到 http://example.com/user/path/to/errorView,但如果您来自 http://example.com/user ,则重定向到 http://example.com/errorView.html。所以只更改最后一个请求路径部分。

    当你使用时:

    return "redirect: /errorView.html"
    

    它指引你

    context path  + new servlet path(/errorView.html)
    

    如果您来自 http://example.com/user/path/to/info ,您将重定向到 http://example.com/errorView.html。不管请求路径有多长。

    更新:如果你看看 this 的优秀帖子,你再也不会遇到相对路径的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 2023-03-10
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多