这是我的第一个项目。我有主页面,当我点击一个链接时,参数(“id_action”)的值在bean中为null。这是Managed Bean的代码:
@ManagedBean(eager=true)
@ApplicationScoped
public class LessonController implements Serializable {
private ArrayList <Lesson> lessons;
private String content ="";
private String name ="";
public LessonController() {
fillLessonAll();
}
private void fillLessonAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
lessons = new ArrayList <Lesson> ();
try {
conn = DataBase.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from lessons");
while(rs.next()){
Lesson lesson = new Lesson();
lesson.setName(rs.getString("name"));
lesson.setLesson(rs.getString("lesson"));
lesson.setId(rs.getInt("idlessons"));
lessons.add(lesson);
}
}catch (SQLException ex){
Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE,null,ex);
} finally {
try {
if(conn!=null) conn.close();
if(stmt!=null) stmt.close();
if(rs!=null) rs.close();
} catch (SQLException ex) {
Logger.getLogger(LessonController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public ArrayList<Lesson> getLessonList (){
return lessons;
}
public void findId() {
Map <String,String> params = FacesContext.getCurrentInstance().getExternalContext().getInitParameterMap();
name = params.get("id_action");
for(Lesson less : lessons) {
if(less.getName().equals(name)) content = less.getLesson();
}
}
public String getContent() {
return content;
}
}
这是 main.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Facelets Template</title>
<h:outputStylesheet library="css" name="main.css"/>
<h:outputStylesheet library="css" name="leftmen3"/>
</h:head>
<h:body>
<ui:composition template="/templates/lesson_template.xhtml">
<ui:define name="content">
<div class="content">
<h:outputText value="#{lessonController.content}" />
</div>
</ui:define>
</ui:composition>
</h:body>
</html>
这是 lesson_template.xhtml
<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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Facelets Template</title>
<h:outputStylesheet library="css" name="main.css"/>
</h:head>
<h:body>
<div>
<ui:insert name="leftmenu">
<ui:include src="../templates/leftmenu.xhtml"></ui:include>
</ui:insert>
<ui:insert name="content">
<ui:include src="../templates/content.xhtml"></ui:include>
</ui:insert>
</div>
</h:body>
</html>
这是 leftmenu.xhtml
<ui:composition>
<h:dataTable value="#{lessonController.lessonList}" var="less" >
<h:column>
<h:form>
<h:commandLink action="#{lessonController.findId()}" value="#{less.name}">
<f:param name="id_action" value="#{less.name}"></f:param>
</h:commandLink>
</h:form>
</h:column>
</h:dataTable>
</ui:composition>
faces-config 很简单:
<?xml version='1.0' encoding='UTF-8'?>
<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>
<resource-bundle>
<base-name>messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
答案 0 :(得分:0)
我在LessonController课程中发现了一个错误!
需要:
Map params = FacesContext.getCurrentInstance()。getExternalContext()。getRequestParameterMap();
代替: Map params = FacesContext.getCurrentInstance()。getExternalContext()。getInitParameterMap()
现在它正在运作。