JSF_First Try

本文介绍了一个使用JSF、Hibernate和Spring技术栈的新项目。通过创建两个托管Bean:helloMessageBean和welcomeBean,演示了如何处理用户输入并显示个性化消息。此外,还展示了index.xhtml和message.xhtml两个页面的设计。

This is a start of a new project in JSF, Hibernate and Spring.


For new JSF project, i would like to start from the design of beans. There are two beans here, which are shown as follows:


package com.cj.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean(name="helloMessageBean")
@RequestScoped
public class helloMessageBean {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String goback() {
		return "index.xhtml";
	}
}
This is the bean for the incoming page and it has only one attribute "name" here.


package com.cj.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean(name="welcomeBean")
@RequestScoped
public class welcomeBean {
	public String sayHello() {
		return "message.xhtml";
	}
}

Following is the design of the relevant xhtml pages -- index.xhtml, message.xhtml. They are as follows:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />        
        <title>JSF2FIRSTTRY</title>
    </h:head>
    
    <h:body>
    	<h:form>
    		<h:outputText value="Please enter into your name:"></h:outputText>
    		<br />
    		<h:inputText id="name" value="#{helloMessageBean.name}" required="true"></h:inputText>
    		<br />
    		<br />
    		
    		<h:commandButton value="submit" action="#{welcomeBean.sayHello}"></h:commandButton>
    		<br />
    		<h:message for="name"></h:message>
    	</h:form>
    </h:body>

</html>

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />        
        <title>JSF2FIRSTTRY</title>
    </h:head>
    
    <h:body>
    	<h:form>
    		<h:outputText value="Come on Boy, #{helloMessageBean.name}!"></h:outputText>
    		<br />
    		<h:commandButton value="Go Back" action="#{helloMessageBean.goback}"></h:commandButton>
    		<h:message for=""></h:message>
    	</h:form>
    </h:body>

</html>


Since the flow control here is done by the action string value and there is no need to configure the faces-config.xml.

The web.xml is as follow:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">

  <display-name>FirstJSF</display-name>
  <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>/faces/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>    
  </welcome-file-list>
</web-app>



Now when we type "localhost:8080/JSFTest1", we will get into our web application face.








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值