Java “返回重定向”的问题&书信电报;url>&“;;在springMVC中

Java “返回重定向”的问题&书信电报;url>&“;;在springMVC中,java,spring,jsp,Java,Spring,Jsp,我正在从事一个使用SpringMVC和Netbeans的项目。我使用bootstrap作为我的CSS(以前有使用PHP的经验)。当用户(管理员)键入URL“”时,他将获得管理员登录表单。我正在使用spring标记来处理表单。处理程序有一个填充“指定”选择框的GET方法和一个处理表单的POST方法。如果他成功登录,那么他将被带到“adminpanel.jsp”。这就是问题的开始。在此页面上,他可以选择更新酒店详细信息、添加房间、添加员工和更新员工角色。所有这些都将使用bootstrap modal

我正在从事一个使用SpringMVC和Netbeans的项目。我使用bootstrap作为我的CSS(以前有使用PHP的经验)。当用户(管理员)键入URL“”时,他将获得管理员登录表单。我正在使用spring标记来处理表单。处理程序有一个填充“指定”选择框的GET方法和一个处理表单的POST方法。如果他成功登录,那么他将被带到“adminpanel.jsp”。这就是问题的开始。在此页面上,他可以选择更新酒店详细信息、添加房间、添加员工和更新员工角色。所有这些都将使用bootstrap modals和spring-form标记完成。每个表单都有一个不同的操作,所有这些操作都将由一个控制器处理(当然,它使用模型属性)。成功登录后,页面不会以“既不绑定结果也不…”的形式打开,我认为这是由于页面在控制器之前加载而引发的异常。我认为,由于EmployeeLoginController返回“adminpanel”,因此EmployeeController永远不会被解雇,因此出现异常。 因此,经过大量研究,我得出结论,也许我需要将控制权从EmployeeLoginController转移到EmployeeController。使用我编写的代码,TomCat无法找到adminpanel.jsp(未找到请求的资源),我看到以下URL:“”。我尝试过使用ModelAndView,但没有效果。谁能帮帮我吗? 谢谢

问题在于:

消息=登录+成功

message=Login+successful
url
/adminpanel
正在查找
@RequestParam(value=“message”)

因此,如果您需要
消息
,只需在控制器方法中添加
@RequestParam(value=“message”)
,使用与否取决于您

如果您在页面登录后不需要该消息,可以通过替换以下内容来使用RedirectView:

return "redirect:/adminpanel";
为此:

RedirectView rw=new RedirectView(request.getContextPath()+"/adminpanel");
rw.setExposeModelAttributes(false);
return rw;
添加

@RequestMapping(method = RequestMethod.GET)
public String viewEmpDetails(@RequestParam(value = "message", required = false) String message,ModelMap model) {
        Employee empForm = new Employee();    
        model.put("empForm", empForm);
        return "adminpanel";
}
@RequestMapping(method = RequestMethod.POST)
public String addEmployee(@RequestParam(value = "message", required = false) String message,@ModelAttribute(value="empForm") Employee employee,ModelMap model) {
        return "adminpanel";
}

我两种方法都试过,但都不管用。在第一次修改中,我对EmployeeController公共字符串viewEmpDetails(ModelMap model,@RequestParam(value=“message”)String msg){Employee empForm=new Employee();model.put(“empForm”,empForm);return“adminpanel”;}或者adminpanel.jsp中的表单处理有问题吗?当我尝试第二种方法时,URL显示“”和“请求的资源不可用”错误。当我尝试第一个时,我得到了最初的错误。更重要的是,使用@RequestMapping(value=“{'/','/emplogin.htm'}”,method=RequestMethod.POST)对您尝试的第二个方法不起作用,它表明您没有上下文路径,所以请尝试首先添加上下文
request.getContextPath()
path并尝试一下,我已经编辑了答案供您参考,您有两个返回adminpanel.jsp的请求映射,因此,我建议将@RequestParam放在这两个位置,并将required放在false,作为回答中的示例。使用第二个方法,我得到了相同的错误“required resource not found”,url为“”。使用第一个,我仍然得到原始的错误消息和url
package HMS.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;
import HMS.DAO.EmployeeDAO;
import HMS.DAO.Employee;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/emplogin.htm"})

public class AdminLoginController {
    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(ModelMap model) {
            Employee adminForm = new Employee();    
            model.put("adminForm", adminForm);
            List<String> roleList = new ArrayList<>();
            roleList.add("Admin");
            roleList.add("Manager");
            roleList.add("Staff");
            model.put("roleList", roleList);
            return "adminportal";
    }
    @RequestMapping(method = RequestMethod.POST)
    public String processLogin(@ModelAttribute(value="adminForm") Employee employee,
            ModelMap model) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("Beans.xml");        
            EmployeeDAO dao=(EmployeeDAO)ctx.getBean("employee");
            Employee emp=new Employee();
            emp.setEname(employee.getEname());
            emp.setEpass(employee.getEpass());
            emp.setErole(employee.getErole());
            Employee status=dao.checkLogin(emp);
            if(status.isValid())
            {
                model.put("message","Login successful");
                return "redirect:/adminpanel";
            }
            else
            {
                model.put("message","Login failed");
                return "redirect:/adminportal";
            }
    }
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<form:form id="frmAddEmp" class="form-horizontal" role="form" method="POST" action="addemployee.htm" modelAttribute="empForm">
</form:form>
package HMS.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;
import HMS.DAO.EmployeeDAO;
import HMS.DAO.Employee;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/addemployee.htm"})

public class EmployeeController {
    @RequestMapping(method = RequestMethod.GET)
    //@RequestMapping(value = "{'/','/addemployee.htm'}", method=RequestMethod.GET)
    public String viewEmpDetails(ModelMap model) {
            Employee empForm = new Employee();    
            model.put("empForm", empForm);
            return "adminpanel";
    }
    @RequestMapping(method = RequestMethod.POST)
    //@RequestMapping(value = "{'/','/addemployee.htm'}", method=RequestMethod.POST)
    public String addEmployee(@ModelAttribute(value="empForm") Employee employee,ModelMap model) {
            return "adminpanel";
    }
}
message=Login+successful
return "redirect:/adminpanel";
RedirectView rw=new RedirectView(request.getContextPath()+"/adminpanel");
rw.setExposeModelAttributes(false);
return rw;
@RequestMapping(method = RequestMethod.GET)
public String viewEmpDetails(@RequestParam(value = "message", required = false) String message,ModelMap model) {
        Employee empForm = new Employee();    
        model.put("empForm", empForm);
        return "adminpanel";
}
@RequestMapping(method = RequestMethod.POST)
public String addEmployee(@RequestParam(value = "message", required = false) String message,@ModelAttribute(value="empForm") Employee employee,ModelMap model) {
        return "adminpanel";
}