SSM實現根據radio實現不同頁面的切換


我提交radio選項是根據button提交申請的。

radio是這么寫的:

<div class="mt-20 skin-minimal" style="text-align: center;">
<div class="radio-box">
<input type="radio" id="radio-2" name="type" value="2" />
<label for="radio-2">員工</label>
</div>
<div class="radio-box">
<input type="radio" id="radio-1" name="type" value="1" />
<label for="radio-1">管理員</label>
</div>
</div>

button是這么寫的:

<div class="row">
<div class="formControls col-8 col-offset-3">
<input id="submitBtn" type="button" class="btn btn-success radius size-L" value="&nbsp;登&nbsp;&nbsp;&nbsp;&nbsp;錄&nbsp;">
</div>
</div>

jsp里關於button是這么寫的:

//登錄
$("#submitBtn").click(function(){
var data = $("#form").serialize();
$.ajax({
type: "post",
url: "login",
data: data,
dataType: "json", //返回數據類型
success: function(data){
if("success" == data.type){
var rv = $("input[name='type']:checked").val();
if(rv=="1"){
window.location.href = "userindex";

}else if(rv=="2"){
window.location.href="employeeindex";
}
//window.parent.location.href = "index";
} else{
$.messager.alert("消息提醒", data.msg, "warning");
$("#vcodeImg").click();//切換驗證碼
$("input[name='vcode']").val("");//清空驗證碼輸入框
}
}
});
});

Controller是這么寫的:

@RequestMapping(value = "/userindex",method=RequestMethod.GET)
public ModelAndView userindex(ModelAndView model){
model.setViewName("system/userindex");
return model;
}

@RequestMapping(value = "/employeeindex",method=RequestMethod.GET)
public ModelAndView studentindex(ModelAndView model){
model.setViewName("system/employeeindex");
return model;
}

話:

由於jsp里是直接跳轉到url,所以Controller必須寫明這兩個,試一下用重定向的方法,在Contrllor里進行判斷后跳轉。

jsp:

window.parent.location.href = "index";

controller:

@RequestMapping("/index")
public ModelAndView index(ModelAndView model,
@RequestParam(value="rv",required=false) String rv){
if(rv=="1"){
model.setViewName("system:userindex");
return model;
}
model.setViewName("system:studentindex");
return model;
}

不知道是不是由於重定向寫的不對,總是執行不了。所以這個方法暫時不成功。

其他:加了信息驗證

針對選擇的【管理員】【員工】的不同,對應驗證其信息。

寫在Controller里:

/**
* 登錄表單提交
* @return
*/
@RequestMapping(value = "/login",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> login(
@RequestParam(value="name",required=true) String name,
@RequestParam(value="password",required=true) String password,
@RequestParam(value="vcode",required=true) String vcode,
@RequestParam(value="type",required=true) int type,
@RequestParam(value="rv",required=false) String rv,
HttpServletRequest request
){
Map<String, String> ret = new HashMap<String, String>();
if(StringUtils.isEmpty(name)){
ret.put("type", "error");
ret.put("msg", "用戶名不能為空!");
return ret;
}
if(StringUtils.isEmpty(password)){
ret.put("type", "error");
ret.put("msg", "密碼不能為空!");
return ret;
}
if(StringUtils.isEmpty(vcode)){
ret.put("type", "error");
ret.put("msg", "驗證碼不能為空!");
return ret;
}
String loginCpacha = (String)request.getSession().getAttribute("loginCpacha");
if(StringUtils.isEmpty(loginCpacha)){
ret.put("type", "error");
ret.put("msg", "長時間未操作,會話已失效,請刷新后重試!");
return ret;
}
if(!vcode.toUpperCase().equals(loginCpacha.toUpperCase())){
ret.put("type", "error");
ret.put("msg", "驗證碼錯誤!");
return ret;
}
request.getSession().setAttribute("loginCpacha", null);
//從數據庫中去查找用戶
if(type == 1){
//if(rv == 1){
//管理員
User user = userService.findByUserName(name);
if(user == null){
ret.put("type", "error");
ret.put("msg", "不存在該用戶!");
return ret;
}
if(!password.equals(user.getUserpassword())){
ret.put("type", "error");
ret.put("msg", "密碼錯誤!");
return ret;
}
request.getSession().setAttribute("user", user);
}
else if(type == 2){
//員工
Employeelogin employeelogin = employeeloginService.findByLoginName(name);
if(employeelogin == null){
ret.put("type", "error");
ret.put("msg", "不存在該員工!");
return ret;
}
if(!password.equals(employeelogin.getLoginpassword())){
ret.put("type", "error");
ret.put("msg", "密碼錯誤!");
return ret;
}
request.getSession().setAttribute("employeelogin", employeelogin);
}
request.getSession().setAttribute("userType", type);
ret.put("type", "success");
ret.put("msg", "登錄成功!");
return ret;
}

由於【員工】【管理員】對應的登錄名和密碼取了不同的名字,前期又是以管理員進行測試,所以@RequestParam還引入了員工的【loginname】和【loginpassword】,但是總出現'GET'的問題,把這兩個注釋掉,就不會出現——原因是在對應的.jsp文件中,並沒有這兩個變量名。

思考后才反應過來,@RequestParam引入的是對應填寫框的變量名,所以我引入的依舊只是(value=“name”),然后針對不同的身份,將name分別傳入到findByUserName(name)findByLoginName(name)來進行驗證。

另外,判斷的條件是type,不能是進行頁面判斷的rv,因為rv=t所選擇的radio的value的值,type就是單純的兩個radio的type值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM