简体   繁体   English

Spring JSP到控制器的重定向错误:此URL不支持HTTP方法POST

[英]Spring jsp to controller redirect error :HTTP method POST is not supported by this URL

I am new to spring and I am creating a project in which i need to pass params from jsp to controller and i am getting HTTP method POST is not supported by this URL error. 我是春季新手,我正在创建一个项目,在该项目中我需要将参数从jsp传递到控制器,并且我正在获取HTTP方法POST。此URL错误不支持POST。 I am providing the right url along with RequestMethod provided over the methods.I dont know why i am getting this error.. 我提供正确的网址,以及通过方法提供的RequestMethod。我不知道为什么会收到此错误。

I have searched all the links like message: HTTP method POST is not supported by this URL but nothing helped me 我已经搜索了所有类似消息的链接:该URL不支持HTTP方法POST,但是没有任何帮助

This is my controller code snippet 这是我的控制器代码段

@Controller
public class LoginController  { 


    @RequestMapping(value="/CheckLogin",method=RequestMethod.POST)
    public String redirecthome(String username,String password,String clientrole) {

      System.out.println("in controller");
        return "mainhome";
    }

This is my jsp 这是我的jsp

<form:form action="CheckLogin" method="POST">

            <h1>
                <!-- <span>Employer</span>  -->
                <lable> Login </lable>
            </h1>
            <div class="inset">
                <p>
                    <label for="email">USERNAME</label> <input name="username"
                        type="text" placeholder="" required />
                </p>
                <p>
                    <label for="password">PASSWORD</label> <input name="password"
                        type="password" placeholder="" required />
                </p>

                <p>

                    <label for="role">User-Role</label> <select id="user-role"
                        name="userrole">
                        <option>Sys-Admin</option>
                        <option>Reseller</option>
                        <option>Client</option>
                    </select>


                </p>
                <!--   <p>
                    <input type="checkbox" name="remember" id="remember">
                    <label for="remember">Remember me</label>
                  </p> -->
            </div>


                <span><a href="#">Forgot password ?</a></span> <input type="submit"
                    value="Login"> <span><a href="RegisterUser.html">SIGN
                        UP NOW!</a></span>

        </form:form>

I guess it's problem with your method args. 我想这是您的方法args的问题。 Try with below segment and see if you are getting the request. 请尝试以下细分,看看是否收到请求。 Ignore clientrole for now. 暂时忽略clientrole。 We can get that fixed later. 我们稍后可以解决该问题。 Spring does not know how to pass username, password as args. Spring不知道如何将用户名,密码作为args传递。 So you need to indicate to get it from request param. 因此,您需要指明从请求参数中获取它。

public String redirecthome(@RequestParam String username,@RequestParam String password,@RequestParam ){
}

I ran this code without any problem using Spring 3.2.5.RELEASE 我使用Spring 3.2.5.RELEASE毫无问题地运行了这段代码

I think this fragment of code is just for test purpose, but if you are trying to implement security It's better to use Spring Security module. 我认为这段代码只是出于测试目的,但是如果您要实现安全性,那么最好使用Spring Security模块。

web.xml web.xml中

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

<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-security.xml 弹簧security.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:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

    <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

    <security:http use-expressions="true" authentication-manager-ref="applicationAuthenticationManager">
        <security:intercept-url pattern="/application/**" access="isAuthenticated()" />

        <security:form-login login-processing-url="/login/login_check.jhtml"
                             authentication-success-handler-ref="applicationLoginSuccessHandler"
                             authentication-failure-handler-ref="applicationLoginFailureHandler" />

        <security:logout logout-url="/logout/logout.jhtml" invalidate-session="true" logout-success-url="/"/>

        <security:access-denied-handler error-page="/forbidden.jhtml" />
    </security:http>

    <bean id="applicationLoginSuccessHandler" class="com.application.ApplicationLoginSuccessHandler" />
    <bean id="applicationLoginFailureHandler" class="com.application.ApplicationLoginFailureHandler" />

    <security:authentication-manager alias="applicationAuthenticationManager">
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select login, passwd, 'true' from user WHERE login = ?"
                authorities-by-username-query="select login, role from user_role WHERE login = ? />
            <security:password-encoder ref="encoder" />
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

login.jsp login.jsp的

<form action="/application/login/login_check.jhtml" method="post">
    <div>
        <label>Login</label>
        <input type="text" name="j_username" value=""/>
    </div>
    <div>
        <label>Password</label>
        <input type="password" name="j_password"/>
    </div>  
    <div>
        <button>Login</button>
    </div>
</form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC JSP Jquery jQuery调用控制器方法单击按钮后发布重定向错误 - Spring MVC JSP Jquery call controller method on button click post redirect error 通过以下形式调用其他jsp:HTTP状态405-此URL不支持HTTP方法POST - Calling other jsp through form : HTTP Status 405 - HTTP method POST is not supported by this URL 为什么会出现错误“此URL不支持HTTP方法发布”? - Why I got the error “HTTP method post is not supported by this URL”? Java:Google App Engine“此URL不支持HTTP方法POST”错误 - Java: Google App Engine “HTTP method POST is not supported by this URL” error Tomcat Servlet返回错误405-此URL不支持HTTP方法POST - Tomcat Servlet returns Error 405 - HTTP method POST is not supported by this URL 在从jsp重定向上调用Spring控制器方法 - Invoke Spring controller method on redirect FROM jsp 从JSP调用servlet时出现“ HTTP状态405 –此URL不支持HTTP方法GET”错误 - “HTTP Status 405 – HTTP method GET is not supported by this URL” error when calling servlet from JSP JBOSS 上的此 URL 不支持 HTTP 方法 POST - HTTP method POST is not supported by this URL on JBOSS WildFly 中的此 URL 不支持 HTTP 方法 POST - HTTP method POST is not supported by this URL in WildFly 此 URL Java servlet 不支持 HTTP 方法 POST - HTTP method POST is not supported by this URL Java servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM