JamesENL
JamesENL

Reputation: 6540

@RequestMapping in Spring Can't get secondary controller to take over

I've got this program I'm writing and I'm trying to get a secondary page to display once the user has logged in. My first screen is handled by the LoginInterfaceController as shown below.

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

Once the user is validated it should redirect them to the /ReferrerHome/ page.

My LeadInterfaceController has its request mappings set as follows

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

However when the user is authenticated, the URL switches to /ReferrerHome/ but the LoginView.jsp is reloaded instead of the home.jsp being loaded. The infuriating thing is that this was working 2 days ago, and I can't think what I did to cause it to break.

I have my LTSServlet.xml here

<?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>

and my web.xml here

<?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>

I am tearing my hair out trying to get this working.

Hope you can help.

Upvotes: 0

Views: 722

Answers (1)

Michal Borek
Michal Borek

Reputation: 4634

You have no mapping to path in validateLogin (only request type). So that when you return "home.jsp" it is mapped to validateLogin and not to @RequestMapping(value="ReferrerHome/home.jsp", method=RequestMethod.POST).

Try: 1. Return ReferrerHome/home.jsp in LeadInterfaceController.handleRequest instead of home.jsp. 2. Change mapping on validateLoginso that it contains path it is bound to (then you can have a descriptive error of your bug after redirection).

That should help.

You can also use debugger on DispatcherServlet and follow the flow (how the "login.jsp" page is chosen).

Upvotes: 1

Related Questions