1

I encounter a strange problem in my Spring MVC Controller.

I have four pages in my webapp folder

enter image description here

@Controller
public class WelcomeController {

    @RequestMapping(value="/wodi/welcome",method=RequestMethod.GET)
    public String welcome(){
        return "redirect:/pages/webwelcome.html";
    }
}

Just now, it worked fine to find the page http://localhost:8080/pages/webwelcome.html, but now I have the error that the browser says:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

I have no idea what I did that influence it.

I read WARN : org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported

But this is not the same case as mine since I am using "GET" method.

Below is my Application.java to Boot the Spring app

@Configuration
@EnableAutoConfiguration
@ComponentScan({"hello","wodinow.weixin.jaskey"})
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

    @Bean
    public CommandService commandService(){
        return CommandService.getInstance();
    }

}
Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • Post configuration file web.xml and spring context.xml file code. – Yagnesh Agola Oct 28 '14 at 09:57
  • @JavaDev, I did not use that, I use Spring Boot and and Application.java to run the program. – JaskeyLam Oct 28 '14 at 10:03
  • On what request do you get the error : `http://localhost:8080/pages/webwelcome.html` or `http://localhost:8080//wodi/welcome` ? And as it looks like you are using eclipse, did you clean and then rebuild the project ? – Serge Ballesta Oct 28 '14 at 10:17
  • @SergeBallesta Both the two URL have the same error,I tried clean the projects, but still the same – JaskeyLam Oct 28 '14 at 10:36

3 Answers3

3

In my case everything was ok. But i have a problem in a controller

that was my problem @RequestMapping( method = RequestMethod.GET)

y change for this:

@RequestMapping(value = "/usuario", method = RequestMethod.GET)

and it works

1

The ViewResolver registered in your application configuration is responsible for resolving pages from a given URL.

Example: Config for resolving URLs like /welcome to corresponding JSP file /pages/welcome.jsp

<bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/pages/" />
  <property name="suffix" value=".jsp" />
</bean>

However, JSP pages are dynamic and need special handling. For static resources like plain html pages, it is sufficient to set up a static mapping for your pages folder.

<mvc:resources location="/pages/" mapping="/**" />

This would result in all resources in the folder /pages being mapped to URLs starting with "/". For example: /pages/welcome.html would be accessible by http://yourdomain/welcome.html

And if you want to set up a view resolver for one specific URL you can use a view-controller in the configuration:

<mvc:view-controller path="/wodi/welcome" view-name="/pages/webwelcome.html"/>

UPDATE:

As you are using Spring Boot with @EnableAutoConfiguration you are already using the second method. Here you can see a code snippet from the AutoConfiguration implementation. It shows that a ResourceHandler is added to URL /** with some predefined locations.

If you want custom URL mappings, I suggest you use one of the above mentioned methods in a plain Spring MVC configuration. Here is the documentation for enabling Spring MVC config. You can decide yourself if you use xml or annotation based configuration.

Jonas
  • 1,315
  • 1
  • 18
  • 31
  • I am not sure what you mean, I did not use any xml to config my app, I do indeed have a web.xml but I did not create that, maven creates it but I do not put anything new in it. Would you please post a complete code sample? – JaskeyLam Oct 28 '14 at 10:06
  • OP goes to the html page through a *redirect* (ok it is unusual) : the view resolver configuration should not be involved. – Serge Ballesta Oct 28 '14 at 10:18
  • @SergeBallesta, would you please show me the way how it is usual to redirect to a html? I am new to Spring MVC, the existing project is built in Servlet before, and now I tried to refactor it into spring – JaskeyLam Oct 28 '14 at 10:37
  • @Jaskey, I edited my answer to give you some examples of possible Spring MVC configurations. – Jonas Oct 28 '14 at 11:32
  • @Jaskey I updated my answer to elaborate on your configuration. – Jonas Oct 28 '14 at 11:51
  • @Jeyp , I am new t Spring MVC, I find it hard to understand you, why the redirect failed? Shouldn't it be working fine? BTW,I do not have any xml file, so I don't konw how to test the solution you mention – JaskeyLam Oct 28 '14 at 12:42
  • @Jaskey - You don't have a xml file because you are using annotation based configuration with Spring Boot. Though using different syntax, both configuration types do the same thing. And with `@EnableAutoConfiguration` Spring Boot applies some default mappings. Can you post some startup logging of your application? – Jonas Oct 28 '14 at 13:57
  • @Thank you ! I found that one of my method(Though it is a post method) is not declared a path, and now I do configure one, and my problem is solved. Would you please sow me an example of how I use configuration(not xml config) to set up a view resolver to a page? – JaskeyLam Oct 28 '14 at 14:04
  • @Jeskey - Yes, you can find one example [here](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-view-controller). It's all in that Spring documentation chapter I linked in my answer. I really suggest you take a deeper look into Spring MVC's documentation. Spring Boot is cool, but it is best suited for zero configuration out-of-the-box functionality. If you want to customize your application, you have to understand Spring MVC in general either way. – Jonas Oct 29 '14 at 09:29
1

Unless you put really weird thing in your config, the (embedded) container should be able to serve static content or JSP that are not under WEB-INF. The only use case where problem could happen would be if you map Spring DispatcherServlet to /* of forget to allow the serving of static resources.

You will find more references on the serving of static resources on my other post Match for root url and serving of static resources.

But usually, in a controller you do not redirect to a HTML page, but give the name of a view and the view resolver finds the appropriate view.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • "you map Spring DispatcherServlet to /* of forget to allow the serving of static resources." What will be the possible way for me to do that? And would you please show me a code sample for the last “solution that but give the name of a view and the view resolver finds the appropriate view” – JaskeyLam Oct 28 '14 at 12:43
  • 1
    Thank you! I find one of my requestmapping method(post method) is not declaring any "value", does it by default route to "/"? And I add the a value="/some_path", and my solution is working! Thank you! If possible, I still hope to get one sample of your last suggestion: give the name of a view and the view resolver finds the appropriate view. – JaskeyLam Oct 28 '14 at 12:48