Scala spring web mvc

I have a scala controller:

@Controller
@RequestMapping(Array("/welcome"))
class HelloController{

  @RequestMapping{val method = Array(RequestMethod.GET)}
  def printWelcome(model: ModelMap) = {
    println("IN: printWelcome(..)")
    val greeting = new GreetingBean("Yo!", "Adam")
    model.addAttribute("message", greeting);
    "secure" // sends to the /jsf/secure.xhtml page
  }

  @RequestMapping{val value = Array("/greeting"), val method = Array(RequestMethod.GET)}
  def greeting(model: ModelMap) = {
    println("IN: greeting(..)")
    val greeting = new GreetingBean("Greetings", "Davies")
    model.addAttribute("greeting", greeting);
    "greeting"; // sends to the /jsf/greeting.xhtml page
  }
}

      

When I call http://localhost:8080/jsf-spring-guice/welcome

, a message is displayed in the console IN: printWelcome(..)

and it is sent to the correct page.

when i call http://localhost:8080/jsf-spring-guice/welcome/greeting

i get error 404

.

I tried to specify @RequestMapping on the greeting method in different ways:

@RequestMapping{val value = Array("greeting"), val method = Array(RequestMethod.GET)}
@RequestMapping{val value = Array("/greeting")}
@RequestMapping(Array("/greeting"))
@RequestMapping(Array("/greeting"), Array(RequestMethod.GET))

      

and decompile the generated class which always looks ok. But I always get approval and always greet / greet / greet.

The decompiled scala class has the following:

@RequestMapping({"/welcome"})

      

and this:

@RequestMapping(value={"/greeting"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})

      

I see no reason why this shouldn't work. Can anyone please help?

+3


source to share


2 answers


Do not check and compile, but the @RequestMapping

annotation for the method printWelcome

must also have the specified value, even if it is empty ("").



The specified value acts as a filter and is likely the cause of the problem.

0


source


solvable ... But couldn't find an answer on Spring documentation.

The situation I had in web.xml

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/welcome/*</url-pattern>
</servlet-mapping>

      

This meant that the display "@RequestMapping (Array (" / welcome ")) had no effect and any request was http://localhost:8080/jsf-spring-guice/welcome

processed with the method marked @RequestMapping{val method = Array(RequestMethod.GET)}

. Default view get

.



It was pointed out that when I entered http://localhost:8080/jsf-spring-guice/welcome/welcome/greeting/

, the method marked was called @RequestMapping{val value = Array("/greeting"), val method = Array(RequestMethod.GET)}

. At this point, it was clear that the double welcome

meant that something was wrong.

Basically, url-pattern

acts like a pre-fix for URLs (we all know that), but the catch is that a controller with a simple GET method will act by default.

Note to yourself do not use a controller with the same request mapping asurl-pattern

0


source







All Articles