Thymeleaf in IntelliJ: cannot resolve variables

Intellij Not Recognizing Model Variables in HTML. How to resolve model variables. I don’t get any idea for this issue.

Here is my Controller

@Controller 
public void someController {
  @RequestMapping("/")
  public String someMethod() {
    model.addAttribute("message", "message");
    return "index";
}

And here is my “index.html”

<p th:text="${message}"> </p>

and of course in my html tag i’m using thymeleaf :

<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">

the problem is in myth:text="${message}" i see red squiggly lines saying that “Cannot resolve “message” variable…”

Solution:

I’ve been ignoring that issue for as long as I’ve been using Thymeleaf. Even though it shows the squiggly lines, it should still work when you run the application.

IntelliJ would almost have to compile the code in the background to be able to automatically (and accurately, since you could have multiple methods who uses the same template) resolve the variables.

I’ve never given a tip like this, but after reading your comment that you just find the wiggly line annoying, I decided to suggest it anyways:

Disable the tip.

configure inspections

disable expression variables validation

I feel absolutely barbaric for posting this answer, forgive me SO

How can i pass two params @Path and @PathParam

I’m trying to create authentification method using java angular js and mysql. I know how to pass one parameter but I couldn’t pass two can you please help me.

Here is my DAO method :

public Client authentifier(int numerocompte,String mdp) {

            System.out.println(numerocompte + mdp);

            try {
            Connection con = Connexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from client WHERE numerocompte = ? AND mdp = ?");
            ps.setInt(1, numerocompte);
            ps.setString(2, mdp);
            Client e = new Client();
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                e.setIdclient(rs.getInt("idclient"));
                e.setNomcomplet(rs.getString("nomcomplet"));
                e.setMail(rs.getString("mail"));
                e.setNumerocompte(rs.getInt("numerocompte"));
                e.setMdp(rs.getString("mdp"));

            }
            rs.close();
            return e;

            }catch (Exception e) {
                System.out.println("Erreur avec  authentifier() -->" + e.getMessage());
                return (null);
            }

        }

Here is my controller method in wehere I would Like to pass the 2 params :

@GET
    @Path("authentifier/{numerocompte}{mdp}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Client authentifier(@PathParam("numerocompte , mdp") int numerocompte, String mdp) {
        ClientDao dao = new ClientDao();
        System.out.println(numerocompte);
        return dao.authentifier(numerocompte,mdp);
    }

and here is the angular js line where also I’ll pass the 2 params :

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte+$scope.nouveauClient.mdp).then(function(data){
                 alert(data.data);
                })

Solution:

All it takes is a additional @PathParam("mdp") annotation in the signature of your controller method.

@GET
@Path("authentifier/{numerocompte}/{mdp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Client authentifier(@PathParam("numerocompte") int numerocompte, 
                           @PathParam("mdp") String mdp) {

    ClientDao dao = new ClientDao();
    System.out.println(numerocompte);
    return dao.authentifier(numerocompte,mdp);
}

Furthermore, make sure you specify your path well. In your example you declared

@Path("authentifier/{numerocompte}{mdp}")

JAX-RS won’t be able to know where numerocompte start and where it ends. Separate them from each other like this

@Path("authentifier/{numerocompte}/{mdp}")

and then

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte+'/'+$scope.nouveauClient.mdp).then(function(data){
             alert(data.data);
})

Spring Architecture Questions: is Service needed?

I’m new to Spring and I’m creating a REST-Service.
I wonder what the Service-Layer is for (@Service).
Currently I encapsulate my DAOs with that Layer.
So for example my PersonController receives a PUT, calls the PersonService which calls the DAO which finally saves the person in my database.

I also saw some examples in the www. (https://howtodoinjava.com/spring/spring-core/how-to-use-spring-component-repository-service-and-controller-annotations/)

I also have read this question here: Is this a good Spring Architecture (include testing)

Do I even need the Services, if it only calls the DAO like:

public void save(Person p) {
    personDAO.save(p);
}

?

I don’t really see what the advantage of this is. I create classes which actually do nothing… Or is it just a architecture standard because additional business logic will be in this layer too?


Also… when I have several services like personService, familyService, animalService, etc. and they all have some method signatures that are the same and some which aren’t… is it useful to use Interfaces here?

When I create Interfaces like: Saveable, Loadable, Deleteable (does this even make sense?) – how do I use them correctly? Do I create a new Interface for every service that extends the Interfaces I desire? Like a PersonServiceInterface that extends Loadable and Deleteable and I implement it in my PersonService?

Thanks in advance!

Solution:

If you do not feel the need for a service layer, then chances are high your controllers are performing way more business logic than they should.

adding a service layer in between allows for dedicated tests of pure frontend (controller handle request and response wiring only / services handle the business logic/ repositories persist and query the data).

so rule of thumb – it is much easier to have many services handle fractions of your logic than to stuff everything into your controllers

i am not sure what you are trying to solve with the interfaces you introduce – i do not think it makes sense to re-invent the Repository interface on top of your services. If your services only forward to repositories directly then there is no use in having them anyway.