【发布时间】: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