无法从Controller重定向到JSP

时间:2015-09-29 07:48:25

标签: java jsp spring-mvc

我正在检查登录凭据,如果它不匹配,则将其发送回index.jsp。但它无法找到index.jsp,因为index.jsp不在其余jsp所在的文件夹Web-INF/views中。我的index.jsp在Webapp中。

在LoginController中,我有这个方法loginFailure(),我希望重定向到index.jsp。我该怎么发送?

的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");
    }**

    }

分配器一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>

1 个答案:

答案 0 :(得分:0)

您可以使用具有分辨率顺序的多视图解析器策略。例如,您可以首先使用XmlViewResolver解析视图是否匹配,否则可以转到internalviewresol。最佳指南是http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/