Template resolver

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Template resolver

Reza
Hi,

I am using Thymeleaf with Spring MVC.

I have a controller action as below :

@ModelAttribute(value="user")
@RequestMapping(value="/user/{id}/{username}",method=RequestMethod.GET)
public User view(@PathVariable("id") int id, @PathVariable("username") String userName)
throws Exception{
        return userRepository.findByUserName(userName);
}

and then I have a file named user.html in my root templates folder but I am getting the following exception.

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "user/21212/reza", template might not exist or might not be accessible by any of the configured Template Resolvers

I tried having a file called view.htm in "user" folder as well. Can anyone advice please?

My apologies if this is a very basic issue but I am new with thymeleaf and spring mvc.
Reply | Threaded
Open this post in threaded view
|

Re: Template resolver

Zemi
Administrator
Hello,

I think you are mixing two Spring concepts in one method.

The @ModelAttribute annotation is used to expose some object to the view.

The @RequestMapping annotation is used to process request and, if you have the stardard Thymeleaf configuration, must return a view name.

Try the following code:

@RequestMapping(value="/user/{id}/{username}",method=RequestMethod.GET)
public String view(@PathVariable("id") int id, @PathVariable("username") String userName, Model model) {
        model.addAttribute("user", userRepository.findByUserName(userName));
        return "user.html";
}

You probably want to do something else with the "id" variable, too.

Reply | Threaded
Open this post in threaded view
|

Re: Template resolver

Reza
Thanks Zemi. I know about the other approach. In fact, I was using something like

@RequestMapping(value="/user/{id}/{username}",method=RequestMethod.GET)
public ModelAndView view(@PathVariable("id") int id, @PathVariable("username") String userName) {
       ModelMap map=new ModelMap();
        User user=userRepository.findByUserName(userName);
        if(user==null)
                throw new NotFoundException("User not found with id : " + id);
        map.addAttribute(user);
        return new ModelAndView("user",map);
}

Which "user" is the name of the view (user.html).

However, I want to use conventional mapping instead of specifying the name of the view.

something like this http://www.springbyexample.org/examples/simple-spring-mvc-form-annotation-config-webapp-jsp-example.html

which is using jsp.
Reply | Threaded
Open this post in threaded view
|

Re: Template resolver

Zemi
Administrator
> I want to use conventional mapping instead of specifying the name of the view

Ok. So you are using the default request to view translator
  http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslator.html

The template name is got from the request mapping. For example, you could map the URL
   http://www.example/com/context/show.html
to the "show" view name.

This philosophy, however, is not compatible with REST-style URL's, because
   http://www.example/com/context/show/123/reza.html
requires the view name "show/123/reza".

You could implement your own ViewResolver, or use an InterceptorHandler to modify the view name stripping last parts, but this could be a bit too overkill. Tell me if you need some help on this matter.

By the way, this is a Spring issue at all.
Reply | Threaded
Open this post in threaded view
|

Re: Template resolver

Reza
Thanks Zemi. I got it now. I will then have to forget about conventional mapping for now.