【发布时间】:2015-03-01 10:24:23
【问题描述】:
我无法通过单击超级扭结来生成动态网址。
这是我的表单的视图,employeeForm.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Employee Form</title>
</head>
<body>
<h2>Employee Information</h2>
<form:form method="POST" action="/result">
Employee ID: <input type="text" name="employeeID">
<br />
Profile Picture: <input type="text" name="profilePicture">
<br />
Name: <input type="text" name="name">
<br />
Date of Birth: <input type="text" name="birthDate">
<br />
Gender: <input type="text" name="gender">
<br />
Address: <input type="text" name="address">
<br />
Phone: <input type="text" name="phone">
<br />
E-mail: <input type="text" name="email">
<br />
Designation: <input type="text" name="designation">
<br />
Job Description: <input type="text" name="jobDescription">
<br />
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
这是 result.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<c:url value="employeeProfile.jsp" var="displayURL">
<c:forEach var="list" items="${list}">
<c:param name="employeeID" value="${list.employeeID}"/>
</c:forEach>
</c:url>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result Form</title>
</head>
<body>
<%--<p>Generated URL is <c:out value="${displayURL}" /> </p>--%>
<a href='<c:out value="${displayURL}" />'> This </a>
</body>
</html>
这是employeeProfile.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>Employee Profile</title>
</head>
<body>
<table>
<c:forEach var="list" items="${list}" >
<tr>
<td><c:out value="${list.employeeID}" /></td>
<td><c:out value="${list.name}" /><td>
<td><c:out value="${list.birthDate}" /><td>
<td><c:out value="${list.gender}" /><td>
<td><c:out value="${list.address}" /><td>
<td><c:out value="${list.phone}" /><td>
<td><c:out value="${list.email}" /><td>
<td><c:out value="${list.designation}" /><td>
<td><c:out value="${list.jobDescription}" /><td>
</tr>
</c:forEach>
</table>
</body>
</html>
这是我的控制器:
package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
@PersistenceContext
EntityManager em;
@RequestMapping(value = "/employeeForm")
public ModelAndView showForm(Model model) {
return new ModelAndView("employeeForm", "command", new Employee());
}
@Transactional
@RequestMapping(value = "/result", method = RequestMethod.POST)
public ModelAndView showResult(@ModelAttribute("")Employee employee, ModelAndView model) {
model.setViewName("result");
em.persist(employee);
System.out.println("persisted");
return model;
}
@Transactional
@RequestMapping(value = "/employeeProfile.jsp", method = RequestMethod.POST)
public ModelAndView showProfile(/*@RequestParam(value = "10", required = false) int employeeID,*/ @PathVariable("id") int id, ModelAndView model)
{
model.setViewName("employeeProfile");
Employee employee=em.find(Employee.class, id);
model.addObject("list", employee);
return model;
}
}
这是生成的输出 url:
http://localhost:8080/employeeProfile.jsp?employeeID=10
取决于我在表单中设置的任何 id。但是我收到一个错误 404 说“请求的资源不可用。”
我知道我显然做错了(或更多)错误,我只是不知道是什么。
【问题讨论】:
-
你的
标签生成了什么值。 -
我已将值编辑到我的问题中。
-
这是因为上下文根不可用。您生成的 url 没有 contextroot。应该是localhost:8080/XXX/employeeProfile.jsp?employeeID=10
-
我没有在您的代码中发现任何错误。我已复制您的代码并运行。它运行没有任何问题。尝试将这一行 java.sun.com/jsp/jstl/core" prefix="c" %> 替换为 java.sun.com/jsp/jstl/core_rt" prefix="c" %>
标签: hibernate jsp spring-mvc jstl