Spring MVC请求映射类级别 [英] Spring MVC request Mapping Class level
本文介绍了Spring MVC请求映射类级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个小型的Spring MVC应用程序.在我的控制器中,我有2种方法返回web-inf文件夹中jsp文件的名称. 现在,该应用程序可以正常运行,但是如果我尝试添加url路径,它将无法正常工作.
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.
我的意思是这样的:
@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";
}
}
这是我的servletConfig配置
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>
如果我给出路径"myApplicationName"/开始/问候,则给出错误.但是,如果我删除开始,它将起作用.
If i give the path "myApplicationName"/start/greeting it give error. But if i remove start it works.
这里似乎是什么问题?
谢谢
更新:
下面是我的web.xml配置
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>
推荐答案
在您的网址格式中添加 / .html * :
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>
这篇关于Spring MVC请求映射类级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文