我已经编写了这个程序,并且正在尝试在用户登录后显示辅助页面。我的第一个屏幕由 LoginInterfaceController 处理,如下所示。
@Controller
public class LoginInterfaceController{
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelMap model = new ModelMap();
logger.info("Returning Login View");
model.addAttribute(getLoginAttempt());
return new ModelAndView("LoginView.jsp", model);
}
@ModelAttribute("LoginAttempt")
public LoginAttempt getLoginAttempt() {
return new LoginAttempt();
}
@RequestMapping(method=RequestMethod.POST)
public String validateLogin(@ModelAttribute("LoginAttempt") @Valid LoginAttempt loginDetails,
BindingResult bindingResult, Model model,
@RequestParam(value="username", required=true) String username,
@RequestParam (value="password", required=true) String password)
{
String returnString;
LoginAttempt checkLogin = new LoginAttempt(username, password);
if(bindingResult.hasErrors())
{
returnString = "LoginView.jsp";
}
else if((!checkLogin.getUsername().equalsIgnoreCase("james") || !checkLogin.getPassword().equals("asdf123")))
{
returnString = "FailedLogin.jsp";
}
else
{
returnString = "redirect:" + "/ReferrerHome/";
}
return returnString;
}
一旦用户被验证,它应该将他们重定向到 /ReferrerHome/ 页面。
我的 LeadInterfaceController 的请求映射设置如下
@Controller
public class LeadInterfaceController {
private LeadServiceClass leadService = new LeadServiceClass();
protected final Log4JLogger logger = new Log4JLogger();
@RequestMapping(value="/ReferrerHome/")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelMap model = new ModelMap();
logger.info("Returning Referrer Home View");
model.addAttribute(getLead());
model.addAttribute(getBrokerList());
return new ModelAndView("home.jsp", model);
}
@ModelAttribute("Lead")
private Lead getLead() {
return leadService.getNewLead();
}
@ModelAttribute("Broker")
private ArrayList<Broker> getBrokerList(/*Referrer referrer*/)
{
return /*(ArrayList<Broker>)referrer.getBrokers() */ new ArrayList<Broker>();
}
@RequestMapping(value="ReferrerHome/home.jsp", method=RequestMethod.POST)
public String validateLogin(@ModelAttribute("Lead") @Valid Lead leadInput, BindingResult bindingResult, Model model)
{
String returnString;
if(leadInput.getLeadHomePhoneNumber() == null &&
leadInput.getLeadWorkPhoneNumber() == null &&
leadInput.getLeadMobilePhoneNumber() == null &&
leadInput.getLeadEmail() == null){
logger.info("HPhoneNum: " + leadInput.getLeadHomePhoneNumber());
//code here to set an error state indicating that some contact information is required
}
if(bindingResult.hasErrors())
{
returnString = "ReferrerHome/home.jsp";
}
else
{
leadService.getLeadDao().addLead(leadInput);
returnString = "ReferrerHome/success.jsp";
}
return returnString;
}
但是,当用户通过身份验证时,URL 会切换到 /ReferrerHome/,但会重新加载 LoginView.jsp 而不是加载 home.jsp。令人气愤的是,这在 2 天前还在工作,我想不出我做了什么导致它崩溃。
我在这里有我的 LTSServlet.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<tx:annotation-driven/>
<jdbc:embedded-database id="LTSDatabase" type="HSQL" />
<context:component-scan base-package="com.au.curtin" />
<import resource="*/WEB-INF/classes/com/au/curtin/leadtrackingsystem.xml" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="LTSDatabase" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="PersistenceAnnotationPostProcessor"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="hibernateExceptionTranslator"
class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
id="PersistenceExceptionTranslator" depends-on="hibernateExceptionTranslator" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value="" />
</bean>
和我的 web.xml 这里
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>LTSServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LTSServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>LoginView.htm</welcome-file>
</welcome-file-list>
</web-app>
我正在撕扯我的头发试图让这个工作。
希望你能帮忙。