繁体   English   中英

返回重定向问题“ <url> ”; 在springMVC中

[英]problems with return redirect “<url>”; in springMVC

我正在使用Spring MVC和Netbeans进行项目。 我正在使用bootstrap作为我的CSS(以前有使用PHP的经验)。 当用户(管理员)键入URL“ http:// localhost:8035 / HMS / adminportal.htm ”时,他将获得一个管理员登录表单。 我正在使用spring标签来处理表格。 该处理程序具有填充“ Designation”选择框的GET方法和处理表单的POST方法。 如果他已成功登录,那么他将被带到“ adminpanel.jsp”。 这是问题开始的地方。 在此页面上,他具有更新酒店详细信息,添加客房,添加员工和更新员工角色的选项。 所有这些都将使用引导程序模态和弹簧形式标签来完成。 每种形式将具有不同的动作,所有动作将由单个控制器处理(当然,该控制器使用模型属性)。 成功登录后,页面不会打开,因为会抛出“既没有绑定结果也没有.....”异常,我相信这是由于页面在控制器之前加载而发生的。 我认为,由于EmployeeLoginController返回“ adminpanel”,所以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>

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

问题在这里:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM