[英]Redirect with 301 status in Spring MVC 4 without ModelAndView
[英]Issue with redirect in ModelAndView in Spring MVC
我有一個簡單的索引頁和控制器。 如果用戶的憑據不匹配,我只希望控制器重定向到索引頁面。 我在控制器中使用ModelAndView作為我的返回類型。
這是我的控制器代碼:
public class LoginController {
@RequestMapping(value="/login",method = RequestMethod.POST)
public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir,Model model) {
InfoEmployee inf = new InfoEmployee();
InfoManager inf2 = new InfoManager();
String uname=req.getParameter("username");
//Putting the username in the session object
String pwd=req.getParameter("pwd");
String dept = req.getParameter("dept");
String name1 = inf.getName();
String message1 = "Welcome "+name1;
String name2 = inf2.getName();
String message2 = "Welcome "+name2;
model.addAttribute("message", "sorry message");
if(uname.equals(inf.getUsername())&&pwd.equals(inf.getPassword())&&dept.equals(inf.getDept()))
{
req.getSession().setAttribute("uname",inf.getName());
return new ModelAndView("employeeLoginResult", "message", message1);
}
else if(uname.equals(inf2.getUsername())&&pwd.equals(inf2.getPassword())&&dept.equals(inf2.getDept()))
{
req.getSession().setAttribute("uname",inf2.getName());
return new ModelAndView("adminLoginResult", "message", message2);
}
else
{
//ModelAndView modelAndView = new ModelAndView();
//modelAndView.setViewName("redirect:/index.jsp");
//redir.addFlashAttribute("message","Sorry");
//return modelAndView;
//redir.addFlashAttribute(message1, "Sorry");
return new ModelAndView("redirect:/index.jsp","message","Sorry username password error");
//return new ModelAndView("Redirect:/index","message","Sorry, user name or password error");
//return new ModelAndView(new RedirectView("index.jsp"),"message","Sorry, user name or password error");
//return new ModelAndView("RedirectToIndex", "message","Sorry, user name or password error");
}
}
我嘗試使用Flash屬性,但URL仍會附加傳遞的屬性。 這是我的索引頁面:
<b><span class="heading">LOGIN USER</span></b>
<div class="container">
<form action="login.html" method="Post">
<div class="form_style">
<input type="text" name="username" placeholder="Enter Username"/>
<input type="password" name="pwd" placeholder="Enter password"/>
<select name="dept">
<option>IT</option>
<option>Admin</option>
<option>HR</option>
<option>Marketing</option>
</select>
<input type="Submit" value="submit">
<%
String message=request.getParameter("message");
%>
<%=message %>
<span>${message}</span>
<span>${message1}</span>
</div>
</form>
</div>
我似乎無法在索引頁中打印通過$ {message}通過重定向發送的消息。 每個方法都會在URL后面附加消息。 我想知道是否可以使用其他方法解決此問題,或者是否有人可以像我使用RedirectAttributes一樣告訴我使用RedirectAttributes的原因,但它仍向URL添加了屬性。
簡而言之,我只想顯示用戶輸入的用戶名或密碼在我的索引頁面中是錯誤的。 如果我可以在沒有重定向幫助的情況下轉到索引頁面,則該建議也將受到贊賞。
任何建議或幫助表示贊賞。 謝謝你也一樣
編輯:
這是我的調度程序servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<!-- <mvc:annotation-driven ignore-default-model-on-redirect="true" /> -->
<!--<context:annotation-config></context:annotation-config> -->
<context:component-scan base-package="com.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
<!-- <mvc:resources location="webapp/WEB-INF/jsp/stylesheets/main.css" mapping="/stylesheets/**"></mvc:resources> -->
</beans>
目錄:
我還沒有嘗試過,但是可以從當前會話中存儲的flashmap訪問flashattributes。 請也嘗試一下,看看是否有附加內容。
鏈接: https : //docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-flash-attributes
將index.jsp
jsp文件夾中,然后像成功場景一樣創建ModelAndView的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.