0

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. I am providing the right url along with RequestMethod provided over the methods.I dont know why i am getting this error..

I have searched all the links like message: HTTP method POST is not supported by this URL but nothing helped me

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

<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>
Community
  • 1
  • 1
kirti
  • 4,499
  • 4
  • 31
  • 60

2 Answers2

1

I guess it's problem with your method args. Try with below segment and see if you are getting the request. Ignore clientrole for now. We can get that fixed later. Spring does not know how to pass username, password as args. So you need to indicate to get it from request param.

public String redirecthome(@RequestParam String username,@RequestParam String password,@RequestParam ){
}
minion
  • 4,313
  • 2
  • 20
  • 35
0

I ran this code without any problem using 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.

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

<?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> 
Marcelo Keiti
  • 1,200
  • 9
  • 10
  • This is just a test code..I just want to get the params to the controller..and thats not working – kirti Feb 13 '15 at 09:00