我在Spring中理解RequestMapping时遇到问题,因为它无法正常工作。 我要做的是将“/”映射到返回“registration.jsp”,而不是向用户显示填写并返回“greeting.jsp”页面的表单。这是我的代码,但是当我启动我的webapp时,我得到的是/显示注册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>
所以基本上当我启动web-app时,它不会进入控制器(“/”)请求映射,但会正确显示注册表单,因为我填充了该方法内部的数据(如何显示右页如果在显示页面后扫描控制器代码?),但它不会进入我在表单操作中调用的方法。我在日志中得到的是:
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>
答案 0 :(得分:0)
我看到的问题是: -
@RequestMapping(value="/registrazioneAction", method=RequestMethod.POST)
映射用于发布请求,但表单已获取
<form role="form" action="/registrazioneAction" method="get">
将方法从“获取”更改为“发布”希望它解决问题
答案 1 :(得分:0)
user3829082使用相同的答案更快(主要是逐行;))
补充:我无法解释为什么你没有在根映射(“/”)中看到任何进展。这对我来说似乎是正确的,它也可以呈现正确的视图。