Spring MVC request Mapping Class level

我的梦境 提交于 2020-05-16 13:33:11

问题


I have made a small spring mvc app. In my controller i have 2 methods which returns the names of the jsp files in the web-inf folder. Now the application works perfectly, but if i try to add a url path it doesn't work.

What i mean is something like this:

@Controller
@RequestMapping("/start")  //if add this it doesn't work
public class SalesController {

@RequestMapping(value = "/greeting")
public String sayGreeting(Model model) {
    model.addAttribute("greetingMsg", "Hello Spring");
    return "welcome";
}

@RequestMapping(value = "/hello")
public String getHello(Model model) {
    model.addAttribute("greeting", "Yo man");
    return "hello";
     }
 }

Here is my servletConfig configuration

<mvc:annotation-driven />
<context:component-scan base-package="com.myCompany" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="WEB-INF/jsps/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

If i give the path "myApplicationName"/start/greeting it give error. But if i remove start it works.

What seems to be the problem here?

Thank you

Update:

Below is my web.xml configuration

<servlet>
    <servlet-name>SpringServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servletConfig.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>SpringServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

回答1:


Add /.html* in your URL Pattern:

<servlet>
    <servlet-name>SpringServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servletConfig.xml</param-value>
    </init-param> </servlet>

<servlet-mapping>
    <servlet-name>SpringServlet</servlet-name>
    <url-pattern>/*.html</url-pattern> </servlet-mapping>


来源:https://stackoverflow.com/questions/33689317/spring-mvc-request-mapping-class-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!