Spring启动没有重定向到所需的页面

时间:2016-10-23 08:41:07

标签: html spring spring-boot

下面是我的Controller类。

@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保留在文档文件夹中,如文档中所示。 当我点击登录按钮时,网址会更改为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>

对于welcomepage,我将index.html放在static中。登录成功后,它应重定向到所需的页面,而不是显示上述错误。

1 个答案:

答案 0 :(得分: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优秀帖子,你再也不会遇到相对路径的问题