【发布时间】:2015-04-01 06:03:23
【问题描述】:
我在理解 Spring 中的 RequestMapping 时遇到了问题,因为它不像我预期的那样工作。 我要做的是映射“/”以返回“registration.jsp”,而不是向用户显示要填写的表单并返回“greeting.jsp”页面。这是我的代码,但是当我启动我的 web 应用程序时,我得到的是 / 显示注册 jsp,当我填充它在控制器方法映射中获得的数据时 / 使用“注册”(我希望它发生在显示之前注册页面,不是吗?),并且在我单击提交后,处理问候页面的控制器方法不会捕获该操作。这是我的代码....
控制器:
package org.fabrizio.lastfmmusicrecommender.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LastFmController {
@RequestMapping("/")
public String showRegistration(){
return "RegistrazioneUtente";
}
@RequestMapping(value="/registrazioneAction", method=RequestMethod.POST)
public String registraUtente(){
return "greetings";
}
}
我的注册jsp:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Registrazione Utente</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Registrazione Utente</h2>
<form role="form" action="/registrazioneAction" method="get">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Enter email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Enter password">
</div>
<div class="checkbox">
<label><input type="checkbox"> Utente Last.fm?</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>LastFmCarMusicRecommender</display-name>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.xml:
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="org.fabrizio.lastfmmusicrecommender.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
所以基本上当我启动网络应用程序时,它不会进入控制器(“/”)请求映射,而是正确显示注册表单,因为我填写了它进入该方法的数据(它如何显示正确的页面,如果它在显示页面后扫描控制器代码?),但它没有进入我在表单操作中调用的方法。我在日志中得到的是:
02/02/2015 11:07:43 - DEBUG - (InternalResourceView.java:170) - Forwarding to resource [/WEB-INF/views/RegistrazioneUtente.jsp] in InternalResourceView 'RegistrazioneUtente'
02/02/2015 11:07:44 - DEBUG - (FrameworkServlet.java:996) - Successfully completed request
感谢您的建议,我尽力解释并附上尽可能多的有用的东西。
编辑:这是 spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="org.fabrizio.lastfmmusicrecommender.controller" />
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans:beans>
【问题讨论】:
标签: java spring jsp spring-mvc servlets