在Spring中将参数从控制器传递到JSP页面

7
我有一个控制器方法如下。
@RequestMapping("/edit/{jobId}")
public String editJob(@PathVariable("jobId") Integer jobId,Model model){
    model.addAttribute("id",jobId);
    return "edit";
}

我将 jobId 传递以获取特定 id 的作业实例,并返回 "edit" 字符串,以便根据 InternalResourceViewResolver 映射到 edit.jsp。但是当我点击链接时,它会跳转到 /edit/44,其中 44 是要编辑的作业的 ID。最终,我收到了未找到资源的错误提示。

home.jsp

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ page session="false"%>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="<c:url value="/resources/css/style.css"/>" />
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<title>Home</title>
</head>
<body id="main">
<div class="container">
    <h2 style="color:white">All posted jobs</h2>
    <c:if test="${empty jobList}">
        <h6>No Job Post Yet</h6>
    </c:if>
    <c:if test="${!empty jobList}">
        <c:forEach items="${jobList}" var="job">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">${job.title }</h3>
                </div>
                <div class="panel-body">${job.description }</div>
                <div class="panel-footer">
                    <a id="link" href="delete/${job.id }">Delete</a> 
                    <a id="link" href="edit/${job.id}">Edit</a>
                </div>
            </div>
        </c:forEach>
    </c:if>
    <section>
        <form:form method="post" action="add" modelAttribute="job"
            class="form-horizontal">
            <div class="form-group" id="addForm">

                <form:label class="control-label" path="title">Title:</form:label>
                <form:input class="form-control" path="title"/>



                <form:label class="control-label" path="description">Description</form:label>
                <form:textarea class="form-control" rows="5" path="description" />



                <button class="btn btn-success">
                    <span class="glyphicon glyphicon-plus-sign"></span> Add a Job
                </button>
            </div>
            <a id="addJob" href="add">+</a>
        </form:form>
    </section>
</div>

JobController.java

package com.job.src;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.job.src.model.Job;
import com.job.src.services.JobService;

@Controller
public class JobController {

@Autowired
private JobService jobService;

@RequestMapping(value= "/")
public String listJobs(Map<String,Object> map){
    map.put("job", new Job());
    map.put("jobList", jobService.listJobs());
    return "home";
}

@RequestMapping(value= "/add", method=RequestMethod.POST)
public String addJob(Job job){
    jobService.addJob(job);
    return "redirect:/";
}

@RequestMapping("/delete/{jobId}")
public String deleteJob(@PathVariable("jobId") Integer jobId){
    jobService.removeJob(jobId);
    return "redirect:/";
}


@RequestMapping("/edit/{jobId}")
public String editJob(@PathVariable("jobId") Integer jobId,Model model){
    model.addAttribute("id",jobId);  
    return "edit";
}
}

edit.jsp

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css"
href="<c:url value="/resources/css/style.css"/>" />
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form method="post" action="editSuccess" modelAttribute="job"
    class="form-horizontal">
    <div class="form-group" id="addForm">

        <form:label class="control-label" path="title">Title:    </form:label>
        <form:input class="form-control" path="title" />



        <form:label class="control-label" path="description">Description</form:label>
        <form:textarea class="form-control" rows="5" path="description" />

        <button class="btn btn-success">
            <span class="glyphicon glyphicon-plus-sign"></span> Add a Job
        </button>
    </div>
</form:form>


你在web.xml中的URL模式是什么? - Pratik
以 /Web-inf/views 为前缀,.jsp 为后缀。 - 01000001
1个回答

6
editJob方法中,您只返回作业的ID与模型属性到edit.jsp页面。但实际上,在edit.jsp页面上,您需要作业对象,因此您需要通过ID获取作业对象并将其添加为模型属性。
@RequestMapping("/edit/{jobId}")
public String editJob(@PathVariable("jobId") Integer jobId,Model model){
    //model.addAttribute("id",jobId); this is wrong
    Job job = jobService.getJobById(jobId);
    //write method in jobservice to get job by id i.e. getJobById(Integer jobId);
    model.addAttribute("job",job)
    return "edit";
}

谢谢Harshal,让我试试这个。实际上,我已经考虑了一段时间了。 - 01000001
@ArpitParasana 怎样在 JSP 页面中显示模型值? - Mehandi Hassan
@MehandiHassan 你是在使用 JSP 和 JSTL 还是仅仅使用 JSP? - Harshal Patil
@HarshalPatil 是的,我正在使用JSTL的JSP。 - Mehandi Hassan
@MehandiHassan 看看这个 https://stackoverflow.com/questions/34157233/jstl-cannot-print-a-list-of-objects/34158780#34158780 - Harshal Patil
1
@HarshalPatil 谢谢伙计 - Mehandi Hassan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接