1

I am unable to post my form in spring mvc 4

User class

public class User {

    private String Username;
    private String Password;

    public User(String username, String password)
    {
        Username = username;
        Password = password;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    public String getUsername() {
        return Username;
    }
    public void setUsername(String username) {
        Username = username;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }

this is my controller method

package com.**.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.**.dao.*;
import com.**.model.User;

@Controller
public class MainController {

    @Autowired
    private UserDAO UserDAO;

    @RequestMapping(value = {"/hello", "/welcome**" }, method = RequestMethod.GET)
    public ModelAndView defaultPage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Login Form - Database Authentication");
        model.addObject("message", "This is default page!");
        model.setViewName("hello");
        return model;

    }

    @RequestMapping(value = "/admin**", method = RequestMethod.GET)
    public ModelAndView adminPage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Login Form - Database Authentication");
        model.addObject("message", "This page is for ROLE_ADMIN only!");
        model.setViewName("admin");

        return model;

    }

    @RequestMapping(value = {"/h", "/login"}, method = RequestMethod.GET)
    public ModelAndView login(@RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid username and password!");
        }

        if (logout != null) {
            model.addObject("msg", "You've been logged out successfully.");
        }
        model.setViewName("login");

        return model;

    }

    //for 403 access denied page
    @RequestMapping(value = "/402", method = RequestMethod.GET)
    public ModelAndView accesssDenied() {

        ModelAndView model = new ModelAndView();

        //check if user is login
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            UserDetails userDetail = (UserDetails) auth.getPrincipal();
            System.out.println(userDetail);

            model.addObject("username", userDetail.getUsername());

        }

        model.setViewName("402");
        return model;

    }

    @RequestMapping(value = "/Account/userManagement", method = RequestMethod.GET)
    public ModelAndView accountPage() {
        ModelAndView model = new ModelAndView();
        model.setViewName("Account/userManagement");
        return model;
    }

    @RequestMapping(value = "Notification/notification", method = RequestMethod.GET)
    public ModelAndView NotificationPage() {
        ModelAndView model = new ModelAndView();
        model.setViewName("Notification/notification");
        return model;
    }

    @RequestMapping(value = "test", method = RequestMethod.POST)
    public ModelAndView register(@ModelAttribute("user-entity") User user, BindingResult result)
    {
        ModelAndView model = new ModelAndView();
        UserDAO.create(user);
        model.setViewName("hello");
        return model;
    }

    @RequestMapping(value = {"/","/Account/registration"}, method = RequestMethod.GET)
    public ModelAndView registerPage()
    {
        ModelAndView model = new ModelAndView("/Account/registration", "user-entity", new User());
        return model;
    }

}

I am able to load the account registration page. But when i submits, it gives me a
HTTP Status 405 - Request method 'POST' not supported

I am unsure what i am doing wrongly.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.**.dao.UserDAO;
import com.**.dao.UserImplDAO;

@EnableWebMvc
@Configuration
@ComponentScan({ "com.**.web.*" })
@Import({ SecurityConfig.class })
public class AppConfig {

    @Bean(name = "dataSource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("root");
        return driverManagerDataSource;
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public UserDAO getUserDAO()
    {
        return new UserImplDAO(dataSource());
    }

this is my form code

    <form:form action="test" method="POST" modelAttribute = "user-entity">
   <td> <td><form:label path="Username">Name:</form:label></td>  
            <td>
            <form:input path="Username"></form:input>
            </td>
          </tr>
          <tr>
            <td>Password:&nbsp;</td>
            <td><form:input type = "password" path = "Password" ></form:input></td>
          </tr>

</form:form>

Exception

Aug 03, 2014 6:12:53 PM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported

Error

type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource.
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
aceminer
  • 4,089
  • 9
  • 56
  • 104

0 Answers0