【问题标题】:Not able to redirect with href无法使用 href 重定向
【发布时间】:2015-12-15 21:49:58
【问题描述】:

我有一个简单的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>

【问题讨论】:

  • 您在浏览器中看到的网址是什么?
  • 是的,在启动项目时我正在获取 url ( localhost:8080/p/ ),这是我的登录页面,它工作正常,但是我在登录页面内使用了一个 href,你可以见上文,单击该 href 我正在获取 url ( localhost:8080/p/pindex.jsp ),但找不到错误资源。
  • jsp文件存放在哪里?您可以使用 \@RequestMapping 访问 login.jsp。如果您访问 index.jsp,请为 index.jsp 使用其他 \@RequestMaping。

标签: java jsp spring-mvc


【解决方案1】:

如果你想将 redirect 从 jsp 页面转移到另一个页面,你可以像这样使用函数 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";

如果你喜欢在你的jsp页面中使用JSTL

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

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

【讨论】:

  • 但是从一个 JSP 重定向到另一个总是需要在控制器内部映射
  • @Amit Kumar 你想从控制器或页面 jsp 重定向
  • 是的,我想将一个 jsp 链接到另一个 jsp,这就是我想要做的,但无法这样做。 Mohammad Faisal 建议我们需要在控制器内部编写映射。
  • 我想从 JSP 页面重定向
猜你喜欢
  • 2015-08-24
  • 2010-10-31
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
相关资源
最近更新 更多