无法将请求从JSF重定向到Spring
我是spring和jsf新手,在提交到spring控制器类之后,我尝试重定向我的jsf页面。然而,当它调用我的控制器方法中的另一个方法时,我能够重定向,从而命中空指针异常。请告知 谢谢, 维诺 Login.xhtml无法将请求从JSF重定向到Spring,spring,jsf,jsf-2,primefaces,Spring,Jsf,Jsf 2,Primefaces,我是spring和jsf新手,在提交到spring控制器类之后,我尝试重定向我的jsf页面。然而,当它调用我的控制器方法中的另一个方法时,我能够重定向,从而命中空指针异常。请告知 谢谢, 维诺 Login.xhtml <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<center>
<ui:composition template="/BasicTemplate.xhtml">
<ui:define name="content">
<h:form>
<h:panelGrid columns="2" bgcolor="yellow" border="10"
style="height: 127px; width: 481px; ">
<f:facet name="header">
<h:outputText value="LOGIN PAGE"></h:outputText>
</f:facet>
<h:outputText id="user" value="Username"></h:outputText>
<h:inputText id="userName" label="userName" value="#{LoginForm.userName}" required="true"
requiredMessage="Username is mandatory" validatorMessage="username is mandatory">
</h:inputText>
<h:message for="userName" style="color: red;" />
<br></br>
<h:outputText id="passWord" value="Password"></h:outputText>
<h:inputSecret id="pass" value="#{LoginForm.passWord}" required="true"
requiredMessage="Password is mandtory">
<f:validateLength minimum="5" maximum="10" />
</h:inputSecret>
<h:message for="pass" style="color: red;" />
</h:panelGrid>
<h:commandButton value="Login" action="#{loginController.validatelogin(LoginForm)}"/>
</h:form>
</ui:define>
</ui:composition>
</center>
</h:body>
</html>
下面是我的jsf配置文件
<faces-config version="2.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<managed-bean>
<managed-bean-name>LoginForm</managed-bean-name>
<managed-bean-class>com.BillingSolution.Form.LoginForm</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>/login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>errorLogin</from-outcome>
<to-view-id>footer.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>/login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>successLogin</from-outcome>
<to-view-id>header.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
</faces-config>
LoginServiceImpl
package com.BillingSolution.Login;
import com.BillingSolution.Form.LoginForm;
import com.BillingSolution.User.LoginDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private LoginDAO logindao;
@Transactional
@Override
public String ValidateLogin(LoginForm request) {
String userName = request.getUserName();
String passWord = request.getPassWord();
System.out.println("=======================================" + userName);
String result = logindao.loginvalidation(userName, passWord);
return result;
}
@Transactional
@Override
public String adduser(LoginForm request) {
String userName = request.getUserName();
String passWord = request.getPassWord();
System.out.println("=======================================" + userName);
String result = logindao.adduser(userName, passWord);
return result;
}
}
applicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="loginservice" class="com.BillingSolution.Login.LoginServiceImpl"></bean>
错误:
<Aug 28, 2014 12:01:57 AM SGT> <Warning> <javax.enterprise.resource.webcontainer.jsf.lifecycle> <BEA-000000> <# {loginController.validatelogin(LoginForm)}: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
javax.faces.FacesException: #{loginController.validatelogin(LoginForm)}: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
Truncated. see log file for complete stacktrace
Caused By: javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:95)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
Truncated. see log file for complete stacktrace
Caused By: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
Truncated. see log file for complete stacktrace
在您的项目中,JSF不知道Spring管理的bean。要做到这一点,您必须配置JSF ELResolver,它将委托给Spring根WebApplicationContext,解析对Spring定义bean的名称引用 在faces-config.xml文件中配置此解析器,如下所示:
<application>
...
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<h:commandButton value="Login" action="#{loginController.validatelogin(LoginForm)}"/>
另一方面,项目中存在许多违反命名约定的情况。您应该考虑重构到标准约定。看起来您的登录服务不是自动生成的。添加loginservice实现和上下文。xml@Jens,如果我使用JSP并使用此外观作为spring jsf集成的示例,这段代码就可以很好地工作,感谢您的帮助,我已将el解析器添加到spring-config.xml中,并在application-context.xml文件下添加了LoginServieImpl类的bean,但仍然遇到了错误。请告知错误为javax.el.PropertyNotFoundException://C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml@33110 action={loginController.validateLoginForm}:无法访问目标,标识符“loginController”解析为空@Vinoth在尝试上述操作后,您可以发布代码吗?@Vinoth堆栈跟踪与代码不匹配
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="loginservice" class="com.BillingSolution.Login.LoginServiceImpl"></bean>
<Aug 28, 2014 12:01:57 AM SGT> <Warning> <javax.enterprise.resource.webcontainer.jsf.lifecycle> <BEA-000000> <# {loginController.validatelogin(LoginForm)}: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
javax.faces.FacesException: #{loginController.validatelogin(LoginForm)}: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
Truncated. see log file for complete stacktrace
Caused By: javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:95)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
Truncated. see log file for complete stacktrace
Caused By: javax.el.PropertyNotFoundException: //C:/Users/Vinoth/Documents/NetBeansProjects/BillingSolution/build/web/login.xhtml @33,110 action="#{loginController.validatelogin(LoginForm)}": Target Unreachable, identifier 'loginController' resolved to null
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
Truncated. see log file for complete stacktrace
<application>
...
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<managed-bean>
<managed-bean-name>myJsfManagedBean</managed-bean-name>
<managed-bean-class>example.MyJsfManagedBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>mySpringManagedBusinessObject</property-name>
<value>#{mySpringManagedBusinessObject}</value>
</managed-property>
</managed-bean>
with "mySpringManagedBusinessObject" defined as Spring bean in applicationContext.xml:
<bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
...
</bean>
<h:commandButton value="Login" action="#{loginController.validatelogin(LoginForm)}"/>