【问题标题】:what is the reason behind "400" bad request in spring mvcspring mvc中“400”错误请求背后的原因是什么
【发布时间】:2018-07-20 15:56:10
【问题描述】:

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

下面是我的控制器类和 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 天的代码,这是我最后的希望,如果有知识的人指出我做错了什么以及如何修复此代码,我将不胜感激。

【问题讨论】:

标签: spring hibernate spring-mvc jsp


【解决方案1】:

在您的情况下,控制器方法中提到的请求参数不存在于请求中。

尝试使用request.getParameter("fieldName");获取参数

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

【讨论】:

  • 对此我感到非常抱歉,但我仍处于 spring mvc 的学习阶段,请您告诉我有关如何使用 request.getParameter("fieldName") 检查是否存在请求的字段的更多信息
  • 在 JSP 页面中,您为所有字段命名,因此在控制器中,通过传递相同的名称,您可以一一获取所有参数以进行检查...只需 google 一下 request.getparameter
猜你喜欢
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2017-12-20
  • 2016-04-20
相关资源
最近更新 更多