Spring jsp to controller redirect error:此URL不支持HTTP方法POST

时间:2015-02-12 17:45:39

标签: java jsp spring-mvc

我是春天新手,我正在创建一个项目,我需要将params从jsp传递给控制器​​,我正在获取 此URL不支持HTTP方法POST 错误。我提供了正确的URL以及通过方法提供的RequestMethod。我不知道为什么我收到此错误..

我搜索了所有链接 message: HTTP method POST is not supported by this URL  但没有任何帮助我

这是我的控制器代码段

@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";
    }

这是我的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>

2 个答案:

答案 0 :(得分:1)

我想你的方法args存在问题。尝试使用以下细分,看看您是否收到了请求。暂时忽略clientrole。我们可以稍后修复。 Spring不知道如何将用户名,密码作为args传递。因此,您需要指示从请求参数中获取它。

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

答案 1 :(得分:0)

我使用Spring 3.2.5.RELEASE

运行此代码没有任何问题

我认为这段代码仅用于测试目的,但如果您尝试实现安全性,最好使用Spring Security模块。

<强>的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>

<强>弹簧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

<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>