Question

Is there any way to remove .action/.html/(.any) extension in Spring MVC like Struts2.

I'm looking for a clean URL in my address bar.

Like: http://localhost:8080/SpringProject/Dashboard
Rather than: http://localhost:8080/SpringProject/Dashboard.html

Solution That I have found :

Added two line in my spring-servlet.xml file

<mvc:default-servlet-handler/>
<mvc:annotation-driven />

and modify my web.xml

 <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

that's all

I also post this answer in my blog javalink2me.blogspot

Was it helpful?

Solution

The support for @Controller and @RequestMapping is provided by Spring by default. However, by enabling mvc:annotation-driven you get support for processing the requests that are mapped to annotated controller methods, such as declarative validation, formatting and conversion service.

In servlet-config.xml

<mvc:default-servlet-handler/> <mvc:annotation-driven />

In WEB.xml

<servlet-mapping>
        <servlet-name>home</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

OTHER TIPS

You just need to configure your controller with the path you want.

@Controller
public class MyController {

    @Inject
    private MyService myService;

    @RequestMapping("/mypath")
    public String showPathWithouteExtension(Model model) {

        return "myjspfile";
    }

}

With a controller like this, you just need to call.

http://server:port/myapp/mypath

and boom, no extension. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top