JSF+Primefaces实现简单的HelloWorld示例

    JSF相关的概念这里就不作赘述了。

    直接切入正题。

    首先,JSF通常运行在GlassFish容器里。如果使用Tomcat可能出现一些奇怪的问题,比如:

     java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet。

    首先,是JSF需要的最基础的包:javaee-web-api和primefaces。两个包都可以在maven仓库找到并下载。关于如何下载jar包,可以参考我的另一篇博客:点击打开链接.

    然后是项目结构。因为是简单的入门程序,所以结构很简单。
    一个受管理的bean和一个xhtml(也就是jsf文件)就可以了。然后还需要配置web.xml。

    然后是我们的代码:

    HelloController代码:

import javax.inject.Named;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;

@Named(value = "helloController")
@RequestScoped

public class HelloController implements Serializable {
    //定义一个变量
    private String helloStr;

    //设置get、set方法,不然不能获取到该变量
    public String getHelloStr() {
        return helloStr;
    }

    public void setHelloStr(String helloStr) {
        this.helloStr = helloStr;
    }

    //设置测试方法
    public void sayHello(){
        String showStr = "Hello : ";
        helloStr = showStr + helloStr;
    }
}

    然后是index.xhtml:

<html  xmlns="http://www.w3.org/1999/xhtml"
       xmlns:p="http://primefaces.org/ui"
       xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>HelloJSF.</title>
</h:head>
<h:body>
    <h:form id="helloText">
        <p:inputText  value="#{helloController.helloStr}"></p:inputText>
        <p:commandButton value="提交" actionListener="#{helloController.sayHello}" update="helloText" />
    </h:form>

</h:body>
</html>

    然后配置我们的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  <!-- 声明Primfaces的主题 -->
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>aristo</param-value>
  </context-param>

  <!-- 配置faces Servlet。JSF本质上也是Servlet拦截访问 -->
  <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>

  <!-- 指定系统首页 -->
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>

</web-app>

    XML各项配置的作用都注释了。


    这样就可以了。运行截图:

    

    输入撒旦,点击提交:

    

    这里说明一下代码:

    在Controller中使用@Named和@RequestScoped注解,可以让我们直接在JSF界面中通过#{Named注解中设置的名称.属性}来访问我们Controller中的属性。其实这里称这个类为Controller有点不妥,这只是一个普通的类。

    然后在界面中我们使用actionListner来监听点击事件,点击之后调用sayHello方法,并使用update指定更新helloText,实现输入框内容的动态刷新。

    

    最后呢,作者水平有限,有什么问题大家可以提出来共同探讨学习。

    差不多就是这样,祝大家学习愉快。谢谢。

                                                                      —— by:轩辚 ——

    

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值