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

How to generate dynamic url from clicking on a hyperlink

我无法通过点击 hyperkink 生成动态 url。

这是我表单的视图,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 说 "The requested resource is not available."

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

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

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

应该可以工作,因为缺少上下文路径。

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

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>

就是这样。感谢大家的帮助!