[英]Unable to redirect from Controller to JSP
I am Checking Login Credentials, if it's not matching m sending it back to index.jsp. 我正在检查登录凭据,如果不匹配,则将其发送回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. 但是它无法找到index.jsp,因为index.jsp不在
Web-INF/views
文件夹中,其余的jsp都位于该文件夹中。 My index.jsp is in Webapp. 我的index.jsp在Webapp中。
In LoginController I have this method loginFailure()
from this i want to redirect to index.jsp. 在LoginController中,我有此方法
loginFailure()
,我想将其重定向到index.jsp。 How can i send it?? 我该如何发送?
LoginController 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 分派器-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 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>
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.
例如,您可以首先使用XmlViewResolver来解决视图是否匹配的问题,然后可以转到internalviewresolver。 The best guide would be http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/
最好的指南是http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.