Spring MVC中“ 400”错误请求背后的原因是什么

时间:2018-07-20 15:56:10

标签: spring hibernate spring-mvc jsp

@Controller
public class UsersController

{
	

	@Autowired
	UsersService userServices;
	@RequestMapping(value="/page", method = RequestMethod.GET) /* whenever the client gives the url request "/page" it will run this method and shows the users.jsp page to the client */
	public ModelAndView getuserPage(){
		ModelAndView view =new ModelAndView("users");
		return view;
	}
	
	@RequestMapping(value="register" ,method = RequestMethod.POST	)// whenever the client gives the url request "users/register" it will run this method and shows the users.jsp page to the client //
	public ModelAndView Registeruser(@RequestParam("user_name") String user_name, @RequestParam("user_contact") int user_contact,@RequestParam("user_email") String user_email, @RequestParam("user_password") String user_password)
	
	{
		Users usersobj=new Users(user_name, user_contact, user_email, user_password);
		
		ModelAndView modelview=new ModelAndView();
		
		if(userServices.saveOrUpdate(usersobj))
		{
		modelview.setViewName("users");
				return modelview;
		}
		else
		{
			modelview.setViewName("users");
			 return modelview;
		}
		
	}
}
	/*
	@ModelAttribute("users_obj")
	public Users constructUser() {
		return new Users();
	}
	@RequestMapping(value="/register.html" ,method = RequestMethod.POST	)// whenever the client gives the url request "users/register" it will run this method and shows the users.jsp page to the client //
	public ModelAndView Registeruser(@ModelAttribute("users_obj") Users users_obj) 
	{		
		ModelAndView modelview=new ModelAndView();
		if(userServices.saveOrUpdate(users_obj))
		{
		modelview.setViewName("users");
				return modelview;
		}
		 modelview.setViewName("users");
		 return modelview;
     }
}
<body>

<div id="login-form">
<!-- gives the toggle effect between login and register -->
<input type="radio" checked id="login" name="switch" class="hide">
<input type="radio" id="signup" name="switch" class="hide">
<!-- bootstrap icons that does not require download or install-->
<div>
<ul class="form-header">
<li><label for="login"><i class="fa fa-unlink"></i> LOGIN</label></li>
<li><label for="signup"><i class="fa fa-credit-card"></i> REGISTER</label></li>
</ul>
</div>

<div class="section-out">
<section class="login-section">
<div class="login">
<form action=""> <!-- on login i have to put the url of the backend class to handle it -->
<ul class="ul-list">
<li><input type="email" required class="input" placeholder="Email ID" id="email"/><span class="icon"><i class="fa fa-user-secret" style="font-size:20px"></i></span></li>
<li><input type="password" required class="input" placeholder="Password" id="pass"/><span class="icon"><i class="fa fa-lock" style="font-size:20px"></i></span></li>
<li><span class="remember"><input type="checkbox" id="check"> <label for="check">Remember Me</label></span><span class="remember"><a href="">Forgot Password</a></span></li><!-- on forgot password i have to put the url of the backend class to handle it -->
<li><input type="submit" value="SIGN IN" class="btn" onclick="validate()"/></li>
</ul>
</form>
</div>

</section>

<section class="signup-section">
<div class="login">
<form action="register" method="post"><!-- on registration i have to put the url of the backend class to handle it -->
<ul class="ul-list">
<li><input type="text" required class="input" placeholder="Your Name" id="R_name" name="user_name"/><span class="icon"><i class="fa fa-user" style="font-size:20px"></i></span></li>
<li><input type="number" required class="input" placeholder="Your Number (no plus or minus signs)" id="R_number" name="user_contact" pattern="('^\\d+$')" title="Must contain only numbers" required/><span class="icon"><i class="fa fa-mobile-phone" style="font-size:25px"></i></span></li>
<li><input type="email" required class="input" placeholder="Your Email" id="R_email" name="user_email"/><span class="icon"><i class="fa fa-envelope" style="font-size:15px"></i></span></li>
 <li><input type="password"  placeholder="Password" required class="input"  id="R_pass" name="user_password" pattern="(?=.*?[0-9]).{8,}" title="Must contain at least one number and at least 8 or more characters" required/><span class="icon"><i class="fa fa-lock" style="font-size:20px" ></i></span></li>
<li><input type="password"  placeholder="Retype Password" required class="input"  id="Rc_pass"pattern="(?=.*?[0-9]).{8,}" title="Must contain at least one number and at least 8 or more characters" required/><span class="icon"><i class="fa fa-lock" style="font-size:20px" ></i></span></li>
<li><input type="checkbox" id="check1"> <label for="check1">I accept terms and conditions</label></li>
<li><input type="submit" value="SIGN UP" class="btn" onclick="validate2()"></li>
</ul>
</form>
</div>


</section>
</div>

</div>
</body>

下面是我的controllerclass和users.jsp文件 在users.jsp文件中,我有不同的形式,一种用于登录,另一种用于用户注册。 我目前正在处理注册表单及其相关的控制器类,并收到此错误

我也尝试过modelattribute并得到了相同的错误消息,所以尝试过@requestparam并仍然遇到相同的错误,我在这里做错了什么,我确实有一个JavaScript来强制客户端不给空参数 我有用户pojo,而saveorupdate类的服务如下:

@Service
public class UsersServiceImpl implements UsersService{

    @Autowired
    UsersDao userDao;

    public boolean saveOrUpdate(Users users) {
        return userDao.saveOrUpdate(users);
    }

请问有人可以帮我指出这段代码有什么问题吗?我在做错什么吗?我是否缺少pom.xml或前端控制器xml文件中的某些配置?我正尝试修复过去2天的代码,这是我最后的希望,如果有知识的人指出我在做什么错以及如何修复此代码,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

在您的情况下,控制器方法中提到的请求参数不随请求一起出现。

尝试使用request.getParameter("fieldName");

获取参数

并检查所有字段是否存在以及类型int或字符串。