我实际上是从 JSF 开始的,所以放轻松。
我正在尝试混合 Spring 3.x + Hibernate 4 + JSF 2.2 我曾经在桌面应用程序中混合 Spring + Hibernate,这根本不是问题。
但是现在,我在这里有点困惑。我正在尝试通过注释进行映射,我很确定那是一团糟。
首先,我想问您集成所有这些技术并保持 MVC 模式的最佳实践
话虽如此,这是我的文件:
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"
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">
<display-name>WebCD</display-name>
<!-- Spring Config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Spring/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
面孔-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
AppMainController.java
@Controller
public class AppMainController {
@Autowired
private CDDao cdDao;
public List<CD> findAllCDs() throws SQLException {
List<CD> cds = cdDao.findAll();
return cds;
}
}
CDDaoImpl.java
@Repository
public class CDDaoImpl extends Dao<CD> implements CDDao {
@Override
public Class<CD> getEntityClass() {
return CD.class;
}
}
最后,*.xhtml 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Lista de CDs</title>
</h:head>
<h:body>
<center>
<h1>Lista de CDS</h1>
</center>
<h:messages />
<h:dataTable value="#{/*Right here I really don't know what to call*/.findAllCDs()}" var="cd"
rules="rows" cellpadding="5">
<f:facet name="caption">Essa é a lista de todos os CD's cadastrados no sistema</f:facet>
<f:facet name="header">Lista de CD's</f:facet>
<f:facet name="footer">Fim da Lista de CD's</f:facet>
<h:column>
<f:facet name="header">Código</f:facet>
#{cd.id}
</h:column>
<h:column>
<f:facet name="header">Descrição</f:facet>
#{cd.descricao}
</h:column>
<h:column>
<f:facet name="header">Gravadora</f:facet>
#{cd.fkLabel}
</h:column>
<h:column>
<f:facet name="header">Preço</f:facet>
#{cd.preco}
</h:column>
<h:column>
<f:facet name="header">Data Compra</f:facet>
#{cd.dataCompra}
</h:column>
<h:column>
<f:facet name="header">Data Gravação</f:facet>
#{cd.dataGravacao}
</h:column>
</h:dataTable>
</h:body>
</html>
我试过这些方法:
- 添加
@ManagedBean(name="ctrl")
到AppMainController
并调用ctrl.findAllCDs()
。 - 创建一个属性
private List<CD> cds
和一个调用方法cdDao.findAll()
来填充列表并检索到 dataTable 作为myBean.cds
.
这些是以下例外:
NullPointerException
: cdDao 在某些时候为空,这导致我考虑唯一的逻辑推断:cdDao
没有被注入(自动连线)@ManagedBean
不是在正确的地方..
如果你能给我一个指导方针,一条路径,我将不胜感激,因为我真的很困惑。