【发布时间】:2014-12-19 07:54:05
【问题描述】:
亲爱的朋友们,
我尝试执行 Spring MVC 表单示例 web 项目。
当我尝试点击带有动作名称的 url 时,相应的视图没有调用 但是如果尝试使用视图名称(/viewname.html)调用 url,它会显示出来。
我看过很多例子,我想知道为什么我的代码没有调用基于 动作名称。
我的示例代码:
控制器:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value="/login",method = RequestMethod.GET)
public String login(ModelMap model){
model.addAttribute("message","Welcome Jagan");
return "login";
}
}
spring-servlet.xml:
我的 servlet 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml: 我的 web xml 看起来像
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVCWithJSP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
login.jsp: 有一些受欢迎的内容
如果我尝试点击以下网址
http://localhost:8082/SpringMVCWithJSP/test/login => not invoking login page.
ttp://localhost:8082/SpringMVCWithJSP/test/login.html => successfully invoking login page .
我的问题是为什么登录操作名称不直接调用 login.jsp 而是仅使用 .../test/login.html 而不是 .../test/login 调用
有什么办法解决这个朋友吗?
【问题讨论】:
-
因为您将 Servlet 映射为仅响应具有
*.html模式的请求。如果您不将.html添加到 URL,则 servlet 将不会监听它们(因为它与模式不匹配)
标签: spring-mvc