JSF 2.0简单示例

和Java EE 6一起来的JSF 2.0虽然动静不是很大,但SUN在Java EE 6 Tutorial中的一些表态比较明显。

 

JSP is considered as a deprecated presentation technology for JavaServer Faces 2.0.


存档个小例子:前端是两个xhtml, request/response, 他们调用后台的managed bean完成逻辑。

其中有些说明:

1、@ManagedBean 表示用于JSF的托管Bean

2、@SessionScoped表示生命周期

3、类名和属性名称在这里首字母小写了

 

Request.xhtml


复制代码
代码
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h
="http://java.sun.com/jsf/html"
      xmlns:f
="http://java.sun.com/jsf/core">
    
<h:body>
        
<h:form>
            
<h:inputText id="x" value="#{calcBean.x}">
                
<f:validateLongRange minimum="#{calcBean.min}" maximum="#{calcBean.max}"/>
            
</h:inputText>
            
<h:inputText id="y" value="#{calcBean.y}">
                
<f:validateLongRange minimum="#{calcBean.min}" maximum="#{calcBean.max}"/>
            
</h:inputText>

            
<h:commandButton id="submit" value="calc" action="response.xhtml"/>
        
</h:form>
    
</h:body>
</html>
复制代码

Response.xhtml


复制代码
代码
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h
="http://java.sun.com/jsf/html">
    
<h:body>
        
<h:outputLabel value="#{calcBean.response}"/>
    
</h:body>
</html>
复制代码

Bean Code

复制代码
代码
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class CalcBean {
    
int x;
    
int y;

    
static final int MAX = 20;
    
static final int MIN = 10;

    
public String note(){
        
return "Calculator";
    }

    
public int getX(){
        
return x;
    }

    
public int getY(){
        
return y;
    }

    
public void setX(int newVal){
        x 
= newVal;
    }

    
public void setY(int newVal){
        y 
= newVal;
    }
    
    
public int getResponse(){
        
return x + y;
    }

    
public int getMax(){
        
return MAX;
    }

    
public int getMin(){
        
return MIN;
    }

}
复制代码


 效果:

1、输入不合规范数据:

 

2、输入合规数据

 

posted @   蜡笔小王  阅读(1335)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示