0
votes

I am working on a project using Spring MVC and Netbeans. I am using bootstrap as my CSS(having previous experience with PHP). When the user(admin) types the URL "http://localhost:8035/HMS/adminportal.htm", he gets an admin login form. I am using spring tags to process the form. The handler has a GET method that populates the "Designation" select box and a POST method that processes the form. If he is successfully logged in, then he will be taken to "adminpanel.jsp". This is where the problem begins. On this page, he has options to update hotel details, add rooms, add employees and update employee roles. All of them will be done using bootstrap modals and spring form tags. Each form will have a different action all of which will be handled by a single controller(which of course uses model attributes). Upon successful login, the page does not open as "Neither binding result nor....." exception is thrown which I believe occurs since the page is loaded before the controller. I think since the EmployeeLoginController returns "adminpanel" so the EmployeeController never gets fired and thus the exception occurs. So, after a lot of research, I came to the conclusion that perhaps, I need to transfer control to the EmployeeController from EmployeeLoginController. With the code that I have written, TomCat cannot find adminpanel.jsp (requested resource not found) and I see the following URL: "http://localhost:8035/HMS/adminpanel?message=Login+successful". I have tried using ModelAndView but to no avail. Can anyone please help me out? Thanks

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

1 Answers

0
votes

The problem is here :

http://localhost:8035/HMS/adminpanel?message=Login+successful.

message=Login+successful

The url /adminpanel is looking for @RequestParam(value = "message")

So if you need the message, just add @RequestParam(value = "message") in your controller method and its up to you to use that or not OR,

if you doesn't need that message after page login, you can use RedirectView by replacing this:

return "redirect:/adminpanel";

with this:

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

Add

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