Hallo, ich bin grundlegende persönliche Blog-Management-System mit Feder mvc und jsp.Ich möchte Seite Info/Fehlerbenachrichtigung zu machen.If wollte Blog ist nicht vorhanden die Fehlermeldung wird httpSession hinzugefügt und die Fehlermeldung wird in JSP-Seite angezeigt .Spring MVC -JSP-Wie kann ich Sitzungsattribut I JSP erhalten?
Ich habe NotificationMessage-Modell, NotificationService-Schnittstelle und es Implementierung wie folgt.
NotificationMessage Klasse
package com.fatih.blogproject.model;
public class NotificationMessage {
private NotificationMessageType type;
private String text;
public NotificationMessage(NotificationMessageType type, String text) {
this.type = type;
this.text = text;
}
public NotificationMessageType getType() {
return type;
}
public String getText() {
return text;
}
}
NotificationService Schnittstelle
package com.fatih.blogproject.service;
public interface NotifiacationService {
void addInfoMessage(String msg);
void addErrorMessage(String msg);
}
Umsetzung der Schnittstelle
package com.fatih.blogproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fatih.blogproject.model.NotificationMessage;
import com.fatih.blogproject.model.NotificationMessageType;
import com.fatih.blogproject.service.NotifiacationService;
@Service
public class NotificationServiceImpl implements NotifiacationService{
//Bu servis bilgi ve error mesajlarını HTTPSession içinde uzun süre tutmaya
göre ayarlı olacak.
//HTTPSession içinde veriler key value çifti olarak tututlurlar.
public static final String NOTIFY_MSG_SESSION_KEY =
"siteNotificationMessages";
@Autowired
private HttpSession httpSession;
@Override
public void addInfoMessage(String msg) {
addNotificationMessage(NotificationMessageType.INFO, msg);
}
@Override
public void addErrorMessage(String msg) {
addNotificationMessage(NotificationMessageType.ERROR, msg);
}
private void addNotificationMessage(NotificationMessageType type , String msg){
List<NotificationMessage> notifyMessage = (List<NotificationMessage>)httpSession.getAttribute(NOTIFY_MSG_SESSION_KEY);
if(notifyMessage==null){
notifyMessage=new ArrayList<NotificationMessage>();
}
notifyMessage.add(new NotificationMessage(type, msg));
httpSession.setAttribute(NOTIFY_MSG_SESSION_KEY, notifyMessage);
}
}
Homecontroller
package com.fatih.blogproject.controller;
import java.util.List;
import java.util.stream.Collectors;
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 org.springframework.web.bind.annotation.SessionAttributes;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fatih.blogproject.model.Post;
import com.fatih.blogproject.service.NotifiacationService;
import com.fatih.blogproject.service.PostService;
@Controller
@RequestMapping("/")
public class HomeController {
private PostService postService;
private NotifiacationService notifyService;
@Autowired
public HomeController(PostService postService , NotifiacationService notifyService) {
this.notifyService=notifyService;
this.postService=postService;
}
@RequestMapping(method=RequestMethod.GET)
public String home(Model model){
List<Post> latest5Posts=postService.findLatest5();
model.addAttribute("latest5Posts",latest5Posts);
List<Post> latest3Posts=latest5Posts.stream()
.limit(3)
.collect(Collectors.toList());
model.addAttribute("latest3Posts", latest3Posts);
return "index";
}
@RequestMapping("/post/{id}")
public String post(@PathVariable("id") Long id ,Model model){
Post post=postService.findById(id);
if(post==null){
notifyService.addErrorMessage(id+"id li post bulunamadı");
return "redirect:/";
}
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(post.getDate());
model.addAttribute("post", post);
model.addAttribute("date",date);
return "post";
}
}
Ich möchte für die Benachrichtigung mesage Sitzungswert zuzugreifen und sie in index.jsp
ansehen Wie kann ich das tun?
Ich versuche, diesen Code in Index-Seite
<%=request.getSession().getAttribute("siteNotificationMessages") %>
aber dieser Code geben Sie mir Ausgabe in Seitenindex als
[[email protected]] folgen
Hinweis: Ich sehe dieses Projekt in einem Tutorial, das unten verlinkt ist.
Die Verwendung Tutorial Thymeleaf statt jsp.The thymeleaf Code in Tutorial wie folgt
<ul id="messages" th:with="notifyMessages=${session[T(blog.services
.NotificationServiceImpl).NOTIFY_MSG_SESSION_KEY]}">
<li th:each="msg : ${notifyMessages}" th:text="${msg.text}"
th:class="${#strings.toLowerCase(msg.type)}">
</li>
<span th:if="${notifyMessages}" th:remove="all" th:text="${session.remove(
T(blog.services.NotificationServiceImpl).NOTIFY_MSG_SESSION_KEY)}">
</span>
</ul>
Wie kann ich das thymeleaf Code umwandeln jsp?
Danke für alles.
Ich versuche diesen Code, aber ich bekomme NullPointerException. –
Ja. Sie haben NPE, wenn die Nachricht nicht festgelegt ist. Fügen Sie einfach den Null-Check hinzu. – StanislavL
danke für deine antwort ich repariere es. –