2016-01-21 83 views
-1

这是我的第一个项目。我有主页,当我点击链接时,参数(“id_action”)的值在bean中为null。这是托管Bean的代码:f:param向bean返回“null”

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

而且脸,配置很简单:

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

'leftmenu.xhtml'中'xmlns:f =“http://java.sun.com/jsf/core”'“的位置? – BalusC

+0

是的,我在leftmenu.xhtml中有此链接 – idammfte

+0

请求参数是否存在于生成的HTML输出和HTTP请求负载中? (在浏览器中按F12) – BalusC

回答

0

我发现了一个bug类LessonController! 。

需要:

地图PARAMS = FacesContext.getCurrentInstance()getExternalContext()getRequestParameterMap();

代替:。 地图PARAMS = FacesContext.getCurrentInstance()getExternalContext()getInitParameterMap()

现在,它的工作。