gpt4 book ai didi

java - Spring中的@RequestMapping无法让辅助 Controller 接管

转载 作者:行者123 更新时间:2023-12-01 14:36:49 28 4
gpt4 key购买 nike

我正在编写这个程序,我正在尝试在用户登录后显示辅助页面。我的第一个屏幕由 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。令人气愤的是,这两天前还在工作,我想不出我做了什么导致它坏了。

这里有我的 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>

我正在绞尽脑汁地试图让它发挥作用。

希望能帮到你。

最佳答案

您没有映射到 validateLogin 中的路径(仅请求类型)。这样,当您返回“home.jsp”时,它会映射到 validateLogin 而不是 @RequestMapping(value="ReferrerHome/home.jsp", method=RequestMethod.POST).

尝试:1. 在LeadInterfaceController.handleRequest中返回ReferrerHome/home.jsp,而不是home.jsp。2. 更改 validateLogin 上的映射,使其包含绑定(bind)到的路径(然后在重定向后您可能会出现错误的描述性错误)。

这应该有帮助。

您还可以在 DispatcherServlet 上使用调试器并遵循流程(如何选择“login.jsp”页面)。

关于java - Spring中的@RequestMapping无法让辅助 Controller 接管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413222/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com