【问题标题】:problems with return redirect "<url>"; in springMVC返回重定向“<url>”的问题;在春季 MVC 中
【发布时间】:2016-03-13 20:18:17
【问题描述】:

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

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <!--<welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>-->
</web-app>

调度程序-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <context:annotation-config />
    <context:component-scan base-package="HMS" />
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <mvc:annotation-driven />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


</beans>

AdminPortal.java

package HMS.Controller;
import HMS.DAO.Employee;
import java.util.ArrayList;
import java.util.List;
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.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/adminportal.htm"})
public class AdminPortal {
        @RequestMapping(method = RequestMethod.GET)
        public String redirectAdminPortal(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";
    }
}

adminportal.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Administration portal</title>
    </head>
    <body>
        <h2 style="color:red">${message}</h2>
        <form:form action="emplogin.htm" modelAttribute="adminForm" method="POST">
            Name:<form:input path="ename" />
            <br>
            Password:<form:password path="epass" />
            <br>
            Designation:<form:select path="erole" items="${roleList}" />
            <br>
            <input type="submit" value="Submit">
            <br> 
        </form:form>

    </body>
</html>

AdminLoginController.java

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";
            }
    }
}

adminpanel.jsp

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

EmployeeController.java

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";
    }
}

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    问题就在这里:

    http://localhost:8035/HMS/adminpanel?message=登录+成功。

    message=Login+successful
    

    网址/adminpanel正在寻找@RequestParam(value = "message")

    因此,如果您需要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 public String viewEmpDetails(ModelMap model,@RequestParam(value = "message") String msg) { Employee empForm = new Employee(); model.put("empForm", empForm);返回“管理面板”;还是 adminpanel.jsp 中的表单处理有问题?
    • 当我尝试第二种方法时,URL 显示“localhost:8035/adminpanel”和“请求的资源不可用”错误。当我尝试第一个时,我得到了原始错误。更重要的是,使用 @RequestMapping(value = "{'/','/emplogin.htm'}", method=RequestMethod.POST) 不起作用
    • 对于你尝试的第二种方法,它表明你没有上下文路径,所以先尝试添加上下文request.getContextPath()路径并尝试一下,我已经编辑了答案供你参考.
    • 对于第一种方法,您有两个返回 adminpanel.jsp 的请求映射,因此,我建议将 @RequestParam 放在两者中,并将 required 设置为 false 作为答案的示例。
    • 使用第二种方法我得到相同的错误“未找到请求的资源”,并且 url 是“localhost:8035/HMS/adminpanel”。使用第一个,我仍然收到原始错误消息和 url
    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2016-09-07
    • 2015-07-25
    • 2011-10-19
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多