Passage de paramètres du contrôleur de jsp au printemps
J'ai un contrôleur de la méthode à suivre.
@RequestMapping("/edit/{jobId}")
public String editJob(@PathVariable("jobId") Integer jobId,Model model){
model.addAttribute("id",jobId);
return "edit";
}
dans lequel je suis de passage le jobId pour obtenir l'instance de l'emploi par l'id et le retour de "modifier" de la chaîne de sorte qu'il cartes à modifier.jsp que par la InternalResourceViewResolver. Mais quand je clique sur le lien, il va modifier /44 dont 44 serait l'id de l'emploi pour lequel le lien modifier appartient. Enfin, j'ai obtenu le message d'erreur indiquant aucune ressource trouvé.
la maison.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";
}
}
modifier.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>
quel est votre modèle d'url de web.xml??
/Web-inf/vues comme préfixe et .jsp comme suffixe.
/Web-inf/vues comme préfixe et .jsp comme suffixe.
OriginalL'auteur Arpit Parasana | 2015-03-31
Vous devez vous connecter pour publier un commentaire.
Dans
editJob
méthode de votre retour uniquement l'id de travail avec l'attribut de modèle àedit.jsp
. Mais en fait sur modifier.page jsp vous avez besoin d'objet de la tâche, donc vous avez besoin pour obtenir l'objet de la tâche par l'id de l'ajouter en tant qu'attribut du modèle.cela fonctionne pour vous
comment faire pour afficher la valeur de modèle dans la page jsp??
êtes-vous à l'aide de JSP avec JSTL (JSP ou seulement?
thnx copain
OriginalL'auteur Harshal Patil