javaweb中使用JSF

1.myEclipse 中新建一个项目后,右键新建---myEclipse--add JSF capabilities..
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  
  <listener> 
	<listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
 </listener> 
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3.faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
 <managed-bean>
  <managed-bean-name>userBean</managed-bean-name>
  <managed-bean-class>cn.edu.jsf.UserBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  
  <managed-property>
   <property-name>name</property-name>
   <property-class>java.lang.String</property-class>
   <value/>
  </managed-property>
  
  <managed-property>
   <property-name>friend</property-name>
   <property-class>cn.edu.jsf.Friend</property-class>
  <null-value/>
  </managed-property>
  
  <managed-property>
   <property-name>converter</property-name>
   <property-class>javax.faces.convert.Converter</property-class>
  <null-value/>
  </managed-property>
 </managed-bean>
 
 <navigation-rule>
  <from-view-id>/add.jsp</from-view-id>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/show.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
 
 <navigation-rule>
  <from-view-id>/show.jsp</from-view-id>
 </navigation-rule>
</faces-config>


4.UserBean.java
/**
 * 
 */
package cn.edu.jsf;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import com.sun.xml.internal.fastinfoset.stax.events.Util;

/**
 * @author Administrator
 *
 */
public class UserBean {
	private java.lang.String name;
	private Friend friend;
	private Converter converter;

	public UserBean() {
	}

	public java.lang.String getName() {
		return name;
	}

	public void setName(java.lang.String name) {
		this.name = name;
	}

	public cn.edu.jsf.Friend getFriend() {
		return friend;
	}

	public void setFriend(cn.edu.jsf.Friend friend) {
		this.friend = friend;
	}

	
	public javax.faces.convert.Converter getConverter() {
		return new Converter(){

			//字符类型转换为 目标类型
			public Object getAsObject(FacesContext context,
					UIComponent component, String value) {
				if(!Util.isEmptyString(value)){
					String [] values=value.split(":");
					Friend friend=new Friend(values[0], Double.parseDouble(values[1]), Integer.parseInt(values[2]));
					return friend;
				}
				return null;
			}

			//目标类型转换为 字符类型
			public String getAsString(FacesContext context,
					UIComponent component, Object value) {
				// TODO Auto-generated method stub
				Friend friend=(Friend)value;
				if(friend!=null){
					return new StringBuilder("Friend[name=").append(friend.getName())
					.append(",height=").append(friend.getHeight()).append(",age=").append(friend.getAge()).toString();
				}else{
					return null;
				}
				
			}
			
		};
	}

	public void setConverter(Converter converter) {
		this.converter = converter;
	}
	public String add(){
		return "success";
	}
}



5.friend.java
package cn.edu.jsf;

public class Friend {
	private String name;
	private Double height;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getHeight() {
		return height;
	}
	public void setHeight(Double height) {
		this.height = height;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Friend() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Friend(String name, Double height, int age) {
		super();
		this.name = name;
		this.height = height;
		this.age = age;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return new StringBuffer(this.name).append(":").append(this.height).append(":").append(this.age).toString();
	}
	
	
}


6.访问
localhost:8080/web_JSF/add.faces

你可能感兴趣的:(JSF)