0

I am Checking Login Credentials, if it's not matching m sending it back to index.jsp. But its unable to find index.jsp because index.jsp is not in a folder Web-INF/views where rest of the jsp's are residing. My index.jsp is in Webapp.

In LoginController I have this method loginFailure() from this i want to redirect to index.jsp. How can i send it??

LoginController

package com.cts.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cts.Services.LoginService;
import com.cts.entity.LoginDetails;
import com.cts.entity.to.LoginTo;

@Controller
public class LoginController {

    @Autowired
    LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
        public ModelAndView login() {
        ModelAndView model = new ModelAndView("login");

        System.out.println("M in controller");
        return model;

    }

    @RequestMapping(value = "/LoginCheck", method = RequestMethod.GET)
        public ModelAndView loginSuccess(@ModelAttribute LoginTo loginto) 
    {
        System.out.println("hiiiiiiiii Value of login id is:"+loginto.getUserid());
        System.out.println("My password is:"+loginto.getPassword());
        boolean result=loginService.loginCheck(loginto);
        if(result==true)
        {
        ModelAndView model = new ModelAndView("loginSuccess");
        System.out.println("M in Login Success controller");
        return model;
        }
        else
        {
            ModelAndView model = new ModelAndView("loginfailure");
            System.out.println("M in Login Failure");
            return model;
        }
    }

    **@RequestMapping(value = "/return", method = RequestMethod.GET)
        public String loginFailure() 
    {
        System.out.println("M in Login Failure controller");
        return("index");
    }**

    }

Dispatcher-Servlet.XML

<?xml version="1.0" encoding="UTF-8"?>
   <beans
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"

        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans">
        <mvc:annotation-driven />

        <context:annotation-config />

        <context:component-scan base-package="com.cts.*" />

        <aop:aspectj-autoproxy />

        <mvc:resources location="/" mapping="/**" />
        <tx:annotation-driven proxy-target-class="true" />
        <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            id="DataSource">

            <property value="com.mysql.jdbc.Driver" name="driverClassName" />

            <property value="jdbc:mysql://localhost:3306/opop" name="url" />

            <property value="root" name="username" />

            <property value="root" name="password" />

    </bean>

    <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
            setting maximum upload size <property name="maxUploadSize" value="1000000000" 
            /> </bean> -->

    <bean
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
            id="newSessionFactory">
            <property name="dataSource" ref="DataSource" />
            <property name="annotatedClasses">

                <list>
                <value>com.cts.entity.LoginDetails</value>
                <value>com.cts.entity.RegistrationDetails</value>

                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
    </bean>
    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
            id="transactionManager">
            <property name="sessionFactory" ref="newSessionFactory" />
    </bean>

    <bean id="h" class="org.springframework.orm.hibernate3.HibernateTemplate"
            autowire="constructor" />
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views" />
            <property name="suffix" value=".jsp" />
    </bean>
  </beans>

LoginFailure.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
   <form action="return">
    <h1>Login Unsuccessful</h1>
    <input type="submit" value="Return to main Page">
   </form>
  </body>
 </html>
Andrea
  • 11,801
  • 17
  • 65
  • 72
Priyanka Taneja
  • 515
  • 1
  • 5
  • 21

1 Answers1

0

You can use a multiple view resolver strategy with an order of resolution. For example you could use XmlViewResolver at first to resolve if a view matches if not it can then go to internalviewresolver. The best guide would be http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/

Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34