如何通过单击超链接生成动态URL

时间:2015-01-03 08:47:52

标签: hibernate jsp spring-mvc jstl

我无法通过点击超链接生成动态网址。

这是我的表单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;
    }

}

这是生成的输出网址:

http://localhost:8080/employeeProfile.jsp?employeeID=10

取决于我在表单中设置的ID。但我收到错误404说&#34;请求的资源不可用。&#34;

我知道我明显做错了什么(或更多),我只是不知道是什么。

3 个答案:

答案 0 :(得分:2)

我通过进行以下更改来解决问题:

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" %>



<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Result Form</title>
  </head>
  <body>
    <table>
      <TH>Id:</th>
      <TH>Name:</th>
      <c:forEach var="list" items="${list}" >
        <tr>
          **<td><a href="<c:url value="/employeeProfile?employeeID=${list.employeeID}"/>"/>${list.name}</td>**
        </tr>
      </c:forEach>
    </table>

  <%--<p>Generated URL is <c:out value="${displayURL}" /> </p>--%>
  <a href='<c:out value="${displayURL}" />'> This </a>
  </body>
</html>

那就是它。谢谢你的帮助!

答案 1 :(得分:0)

我注意到生成的URL没有通常是项目名称的上下文路径 检查生成的网址

答案 2 :(得分:0)

<强> / <a href='**/**<c:out value="${displayURL}" />'

应该有效,因为缺少上下文路径。

相关问题