重定向傳值


登錄頁面
* 重定向的時候向頁面攜帶數據 要求如下
1.在目標controller方法使用RedirectAttributes類型的參數
2.要求不能直接重定向到頁面,必須經過springmvc的映射

比如傳錯誤信息
先在springmvc配置文件中配置一個解析器
<!--為了controller能攜帶數據 ,經過springmvc的映射 重定向到頁面 -->
    <mvc:view-controller path="/login" view-name="login"></mvc:view-controller>

舉例子 一個登陸頁面controller層

參數中應用了 RedirectAttributes 參數 進行傳值

//驗證碼 源碼里面還存了一份在session域中, 用來和用戶輸入的驗證碼做對比 ,判斷是否輸入正確。 接收從頁面傳過來的值和用戶名,密碼
    //這個鍵 是工具類里面存到域中的那個鍵 ,根據這鍵取session域中的驗證碼值
    @RequestMapping(value = "/login", method = RequestMethod.POST)
public  String  login(Employee employee, String code, HttpSession session, RedirectAttributes attributes){
        String validateCode = (String)session.getAttribute("validateCode");
        session.removeAttribute("validateCode");
        if(!validateCode.equalsIgnoreCase(code)){
            attributes.addFlashAttribute("errorMsg","驗證碼錯誤");
            return "redirect:/login";
        }
        //如果驗證碼正確,驗證用戶名和密
        Employee emp = employeeService.login(employee);
        if(emp != null){
            session.setAttribute("loginUser",emp);
            return "redirect:/index.jsp";
        }else{
            attributes.addFlashAttribute("error","用戶名或密碼錯誤");
            return "redirect:/login";
        }
    }

然后是登錄頁面

<TABLE id=table2 cellSpacing=1 cellPadding=0 width="100%"
                                       border=0>
                                    <TBODY>
                                    <TR>
                                        <span style="color:red">${error}</span>
                                        <TD align=middle width=81><FONT color=#ffffff>用戶名:</FONT></TD>
                                        <TD><INPUT class=regtxt title=請填寫用戶名 maxLength=16
                                                   size=16 value=username name=username></TD>
                                    </TR>
                                    <TR>
                                        <TD align=middle width=81><FONT color=#ffffff>密&nbsp;
                                            碼:</FONT></TD>
                                        <TD><INPUT class=regtxt title=請填寫密碼 type=password
                                                   maxLength=16 size=16 name=password id=pass></TD>
                                    </TR>
                                    <TR>
                                        <TD align=middle width=81><FONT color=#ffffff >驗證碼:</FONT></TD>
                                        <TD><INPUT  title=請填寫驗證碼 maxLength=50 size=12  name=code  value="${errorMsg}">
                                            <span><img id="validateCode" src="${pageContext.request.contextPath}/code/getCode?time="+(new Date().getTime()) ></span></TD>
                                    </TR>
                                    </TBODY>
                                </TABLE>

 




免責聲明!

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



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