Spring MVC:Problème d'affichage avec Expression Language

Fermé
Streamooc Messages postés 79 Date d'inscription samedi 17 juin 2017 Statut Membre Dernière intervention 8 février 2023 - 27 janv. 2019 à 02:02
Bonjour à tous.Je suis bloqué depuis une semaine sur le développement d'une application avec Spring MVC.Mon problème c'est que je dois afficher un message d'erreur dans la page JSP lorsque l'utilisateur connecté n'a pas le droit.J'ai bien fait les imports des librairies jstl et de spring dans les pages JSP mais toujours le message d'erreur ne s'affiche pas.ceci est la condition posée: <c:if test="${err!=null}">
<p>${err}</p>
</c:if>
J'ai besoin d'aides s'il vous plait c'est urgent.

//index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Login-Contact application</title>
<link href="<%=request.getContextPath()%>/static/css/style.css"
rel="stylesheet" type="text/css" />
</head>
<body background="<%=request.getContextPath()%>/static/images/bg.jpg">
<table border="1" width="80%" align="center">
<tr>
<td height="80px">
<%--Header --%> <jsp:include page="include/header.jsp" />
</td>
</tr>
<tr>
<td height="25px">
<%--Menu --%> <jsp:include page="include/menu.jsp" />
</td>
</tr>
<tr>
<td height="350px" valign="top">
<%--Page Content Area --%>
<h3>User Login</h3>
<c:if test="${err!=null}">
<p>${err}</p>
</c:if>
<f:form action="login" modelAttribute="command">

<table border="1">
<tr>
<td>Username</td>
<td><f:input path="loginName" /></td>
</tr>
<tr>
<td>Password</td>
<td><f:password path="password" /></td>
</tr>
<tr>
<td colspan="2" align="right">
<button>Login</button> <a href="#">New User Registration</a>
</td>
</tr>
</table>
</f:form>

</td>
</tr>
<tr>
<td height="25px">
<%--Footer --%> <jsp:include page="include/footer.jsp"></jsp:include>
</td>
</tr>
</table>
</body>
</html>



//JSP------>Controller
@RequestMapping(value="/login",method=RequestMethod.POST)
public String handleLogin(@ModelAttribute("command") LoginCommand cmd,Model m) {
try {
User loggedInUser=userService.login(cmd.getLoginName(), cmd.getPassword());
if (loggedInUser==null) {
//FAILED
//add error message and go back to login-form
m.addAttribute("err","Login failed! Enter valid credentials.");
return "index";//JSP-Login FORM
}

} catch (UserBlockedException ex) {
m.addAttribute("err",ex.getMessage());
return "index";//JSP-Login FORM
}
}


package sn.ezeon.capp.service;

import java.util.List;

import sn.ezeon.capp.domain.User;
import sn.ezeon.capp.exception.UserBlockedException;

/**
  • @author root

*
  • /

public interface UserService {
public static final Integer LOGIN_STATUS_ACTIVE=1;
public static final Integer LOGIN_STATUS_BLOCKED=2;

public static final Integer ROLE_ADMIN=1;
public static final Integer ROLE_USER=2;

/**
  • @param loginName
  • @param password
  • @return
  • @throws UserBlockedException when user account is blocked
  • /

public User login(String loginName,String password) throws UserBlockedException;


}

public class UserBlockedException extends Exception{

/**
  • Creates User object without error description
  • /

public UserBlockedException() {

}
public UserBlockedException(String errDesc) {
super(errDesc);
}

}


public class LoginCommand {
private String loginName;
private String password;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


public class User {
private Integer userId;
private String name;
private String phone;
private String email;
private String address;
private String loginName;
private String password;
private Integer role;
private Integer loginStatus;


public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public Integer getLoginStatus() {
return loginStatus;
}
public void setLoginStatus(Integer loginStatus) {
this.loginStatus = loginStatus;
}


}


//pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sn.ezeon</groupId>
<artifactId>springContactApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springContactApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>

<!--Spring dependencies -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<!-- Connexion pool -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>

<!-- Connexion with database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springContactApp</finalName>
</build>
</project>