@RequestMapping在春季3.2.8

时间:2015-11-10 13:00:43

标签: spring spring-mvc

我在Spring Framework MVC应用程序(版本3.2.8)中有这个控制器,有两种不同的方法:1用于GET,另一个用于POST

@Controller
public class ManageAccountController {

    private static final Logger LOGGER = Logger.getLogger   (ManageAccountController.class);

    //private static final String USER="userBean"; 

    @Autowired
    private UserService userService;

    /**
     * 
     * @param request the http servlet request.
     * @param model the spring model.
     * 
     */ 
    @RequestMapping(value = "/accounts/manageaccount.do", method = RequestMethod.GET)
    public String showForm  (   @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand,
                                BindingResult result, 
                                HttpServletRequest request, 
                                Model model, 
                                Locale locale) {

        dataAccountCommand.setUserBean(getUser(request));

        return "registerAccountView";
    }




    @RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
    private String saveAccount  (   @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand, 
                                    BindingResult result,   
                                    HttpServletRequest request, 
                                    Model model, 
                                    Locale locale) {

        return "registerAccountView";

    }
}

关键是当我把它放在浏览器中时

http://127.0.0.1:7001/devices_admin/accounts/manageaccount.do 

我被重定向到jsp,但是当我把

http://127.0.0.1:7001/devices_admin/accounts/saveaccount.do

我有这个错误

URL: /devices_admin/accounts/saveaccount.do
???error404.error??? 

从jsp调用它会给我相同的结果:

<form:form  commandName="dataAccountCommand" 
                name="dataAccountForm" 
                id="dataAccountForm"  
                method="post" 
                action="${pageContext.servletContext.contextPath}/accounts/saveaccount.do"                      
                htmlEscape="yes">

</form:form>

1 个答案:

答案 0 :(得分:1)

您无法直接从浏览器的URL栏调用POST方法。如果你在URL栏中放了一些东西,你就是在调用GET mehtod。

相反,您必须创建一个带有表单

的页面
<form method="POST" action="http://127.0.0.1:7001/devices_admin/accounts/saveaccount.do">

...

</form>

或者您可以在浏览器中安装REST客户端,并使用POST方法直接进行调用。