无法使用href重定向

时间:2015-12-15 13:03:31

标签: java jsp spring-mvc

我有简单的spring项目和两个jsp页面(index.jsp和login.jsp),login.jsp中有href重定向到index.jsp。但我无法重定向并获取错误HTTP状态404请求的资源不可用。 请找到附带的代码:

Homecontroller.java

package n.k.p;

import java.text.DateFormat;    
import java.util.Date;    
import java.util.Locale;    
import org.slf4j.Logger;    
import org.slf4j.LoggerFactory;    
import org.springframework.stereotype.Controller;    
import org.springframework.ui.Model;    
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller

public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);           
        String formattedDate = dateFormat.format(date);         
        model.addAttribute("serverTime", formattedDate );           
        return "login";
    }       
}

的login.jsp

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Registration</title>
</head>

<body>

    <form method="post" action="registration.jsp">

        <table border="1" width="30%" cellpadding="5">

            <thead>

                <tr>
                    <th colspan="2">Enter Information Here</th>
                </tr>

            </thead>

            <tbody>

                <tr>
                    <td>First Name</td>

                    <td><input type="text" name="fname" value="" /></td>
                </tr>
                <tr>
                    <td>Last Name</td>

                    <td><input type="text" name="lname" value="" /></td>
                </tr>

                <tr>
                    <td>Email</td>

                    <td><input type="text" name="email" value="" /></td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="pass" value="" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit" /></td>

                    <td><input type="reset" value="Reset" /></td>
                </tr>
                <tr>
                    <td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
                </tr>
            </tbody>
        </table>

    </form>
</body>

1 个答案:

答案 0 :(得分:0)

如果你想从jsp页面到另一个页面redirect,你可以使用这样的函数javascript

   ....
  <a href="login.jsp">Login Here</a>
 </form>
 ...
<script>
$('#form a').click(function(e)
{
   e.preventDefault();
  var destination = $(this).attr('href');
   $('#form').attr('action', destination);
   $('#form').submit();
 });
</script>

但是如果你想从控制器重定向尝试使用它:

    return "redirect:/login.jsp";

如果您希望在yuor jsp页面中使用JSTL

   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

   <body>
    <c:redirect url="/login.jsp"/>
    </body>