我们如何在 Spring MVC 中调用 requestmethod=POST?

Posted

技术标签:

【中文标题】我们如何在 Spring MVC 中调用 requestmethod=POST?【英文标题】:How can we call requestmethod=POST in Spring MVC? 【发布时间】:2017-03-03 20:04:23 【问题描述】:

我正在尝试发布“fromdate”和“todate”来获取消息。当我运行下面的代码时,它说“noHandlerFound”。所有 get 方法都可以正常工作,但 Post 方法不行。

html

<form name="filtersForm" action="/SpringApp/getmessages" method="post">
    From Date: <input name="fromDate" type="date" value="01-01-2015" />
    To Date: <input name="toDate" type="date" value="01-03-2015" />
    <input name="submit" type="submit" value="submit" />
</form>

弹簧控制器代码:

@RequestMapping(value = "/getmessages", method = RequestMethod.POST)
public String getMessages(@RequestParam("fromDate") String start_dt, 
                          @RequestParam("toDate") String end_dt,
                          ModelMap model) 
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    Date fromDate, toDate;
    try 
        fromDate = sdf.parse(start_dt);
        toDate = sdf.parse(end_dt);
        model.put("messagesList", appservice.getMessages(fromDate, toDate));
     catch (ParseException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    

    return "home";

当我运行此代码时,出现以下错误:

org.springframework.web.servlet.PageNotFound noHandlerFound

警告:在名称为“mvc-dispatcher”的 DispatcherServlet 中未找到带有 URI [/SpringApp/403] 的 HTTP 请求的映射

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"
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>MonitorApp</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml,
        /WEB-INF/applicationContext.xml,
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Spring 安全性:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd">

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/**" requires-channel="http" />
    <intercept-url pattern="/"
        access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" />
    <intercept-url pattern='/login' access='isAnonymous()' />
    <!-- access denied page -->
    <access-denied-handler error-page="/403" />

    <form-login login-page="/login" default-target-url="/home"
        authentication-failure-url="/login?error" login-processing-url="/j_spring_security_check"
        username-parameter="username" password-parameter="password" />

    <logout logout-success-url="/login?logout" logout-url="/j_spring_security_logout" />
    <!-- enable csrf protection -->
    <csrf />
</http>


<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="admin" password="admin" authorities="ROLE_ADMIN" />
            <user name="user" password="user" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
    http://activemq.apache.org/schema/core
    http://activemq.apache.org/schema/core/activemq-core-4.3.xsd
    http://www.springframework.org/schema/jms 
    http://www.springframework.org/schema/jms/spring-jms-4.3.xsd">

<context:annotation-config />
<context:component-scan base-package="com.someapp" />
<tx:annotation-driven />
<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@172.28.12.31:1521:fsdev" />
    <property name="username" value="cybercodesqat" />
    <property name="password" value="welcome123" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:persistenceUnitName="MonitorJpaPersistenceUnit" />

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />

<tx:annotation-driven transaction-manager="transactionManager" />

【问题讨论】:

请提供minimal reproducible example。 403路径从何而来? 你能显示你的 web.xml 吗? 403 来自 spring security xml web.xml 文件已添加。 你的问题有点明显,你已经配置了 spring security 以在访问被拒绝的情况下抛出错误页面' ' 所以如果您重新检查您的错误,它会显示“未找到带有 URI [/SpringApp/403] 的 HTTP 请求的映射”,这意味着您尚未配置控制器来响应错误页面。所以2个问题,第一个是由于某种原因你试图提交一个表单而不首先进行身份验证,其次你必须配置错误403页面...... 【参考方案1】:

试试这个here 提出的解决方案。

@Configuration
public class CreateYourSecurityConfig extends WebSecurityConfigurerAdapter 
  @Override
  protected void configure(HttpSecurity http) throws Exception 
    http.csrf().disable();
  

【讨论】:

【参考方案2】:

我的问题通过将上下文路径更改为 getmessages 得到解决?_csrf=a0008ebb-bed2-4245-89da-b012db364e99 可能是因为安全问题。

【讨论】:

以上是关于我们如何在 Spring MVC 中调用 requestmethod=POST?的主要内容,如果未能解决你的问题,请参考以下文章

spring @RequestMapping注解技巧

Spring Webflux - 01 MVC的困境

Spring Webflux - 01 MVC的困境

Spring Webflux - 01 MVC的困境

如何使用从 Spring MVC 发回的 JSON 对象填充 jQuery 数据表的行?

如何在 Spring MVC-Spring Security 应用程序中处理 GWT RPC 调用的会话过期异常