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>
<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>
<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;
}
}
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、输入合规数据
贸易电子化,技术全球化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述