实例:MyEclipse开发JSF:html
1:建立Java Web项目名称为JSFTest。java
2:右击项目MyEclipse----Add JSF Capabilites...----出现以下图:web
finish后打开faces-config.xml,在大纲视图出现以下图:数据库
右击Managed Beans以下图:浏览器
以下图:tomcat
以下填写:服务器
为Bean添加属性,右击:app
以下配置:框架
再来一个属性:jsp
此时的UserBean以下:
添加方法hello以下:
public java.lang.String hello(){
if(this.getName().equals("")){
return "fail";
}
else{
this.setWelcome("欢迎,"+this.getName());
return "login";
}
}
右击Managed Beans以下图:
以下图:
以下填写:
为Bean添加属性,右击:
以下配置:
再来一个属性:
此时的UserBean以下:
添加方法hello以下:
public java.lang.String hello(){
if(this.getName().equals("")){
return "fail";
}
else{
this.setWelcome("欢迎,"+this.getName());
return "login";
}
}
点击J图形工具以下图:
而后再配置文件里面单击出现以下图:
配置第一个JSP文件index.jsp
将原本的代码删除而后填上以下代码:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>第一个JSF程序</title>
</head>
<body>
<f:view>
<h:form>
请输入姓名:<h:inputText value="#{user.name}"/><p>
<h:commandButton value="提交" action="#{user.hello}"/>
</h:form>
</f:view>
</body>
</html>
以下图:
在重复上面的步骤来一个welcome.jsp填上以下代码:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>第一个JSF程序</title>
</head>
<body>
<f:view>
<h:outputText value="#{user.welcome}"/>
</f:view>
</body>
</html>
两个JSP以下图:
单击以下图工具:
先点击index.jsp再点击welcome.jsp出现以下,配置login:
点击两下index.jsp出现以下图配置fail:
获得的配置图形: