<?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:h="http://java.sun.com/jsf/html" lang="en"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>シンプルコメント</title> </h:head> <h:body> <h:form> コメント欄 : <h:inputText value="#{commentController.comment}"/> <br/> <h:commandButton value="投稿" action="#{commentController.doPublish}"/> </h:form> </h:body> </html>
package com.example.jsfexam.web; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; /** * * @author atsushi */ @ManagedBean @RequestScoped public class CommentController { private String pComment; public String getComment() { return pComment; } public void setComment(final String comment) { this.pComment = comment; } public String doPublish() { return "simpleResult.xhtml"; } }
<?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:h="http://java.sun.com/jsf/html" lang="en"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>シンプル投稿結果</title> </h:head> <h:body> <h:form> 投稿ありがとうございました : <br/> <h:outputText value="#{commentController.comment}"/> <br/> <h:link outcome="simpleComment" value="戻る" /> </h:form> </h:body> </html>
<h:commandButton value="投稿" action="#{commentController.doPublish}"/>
で、commentController の doPublish() が呼び出される。
ボタンではなく、リンクで呼び出したいときは
<h:commandLink value="投稿" action="#{commentController.doPublish}"/>
<h:inputText value="#{commentController.comment}"/>
で、commentController の setComment() が呼び出される
public class CommentController { public String doPublish() { return "simpleResult.xhtml"; } }
h:commandButton> から呼び出されるメソッドの返値の型は String で、xhtml 名を返す。
<h:outputText value="#{commentController.comment}"/>
で、commentController の getComment() が呼び出される
<h:link outcome="simpleComment" value="戻る" />
で、simpleComment.xhtml が呼び出される
WebContent | +-simpleComment.xhtml | +-sub | +-simpleReuslt.xhtml
のように、配置した場合には、どのようにリンクを設定すれば良いか?
<?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:h="http://java.sun.com/jsf/html" lang="en"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>シンプルコメント</title> </h:head> <h:body> <h:form> コメント欄 : <h:inputText value="#{commentController.comment}"/> <br/> <h:commandButton value="投稿" action="#{commentController.doPublish}"/> </h:form> </h:body> </html>
package com.example.jsfexam.web; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; /** * * @author atsushi */ @ManagedBean @RequestScoped public class CommentController { private String pComment; public String getComment() { return pComment; } public void setComment(final String comment) { this.pComment = comment; } public String doPublish() { return "/sub/simpleResult.xhtml"; } }
<?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:h="http://java.sun.com/jsf/html" lang="en"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>シンプル投稿結果</title> </h:head> <h:body> <h:form> 投稿ありがとうございました : <br/> <h:outputText value="#{commentController.comment}"/> <br/> <h:link outcome="/simpleComment" value="戻る" /> </h:form> </h:body> </html>
package com.example.jsfexam.web; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "myCommentController") @RequestScoped public class CommentController { 〜略〜 }