A small example of getting started with JSF

Source: Internet
Author: User
Tags locale

Consider yourself a JSF little example written in the video tutorial (with internationalization and validation), put it here and share:

automatically import the appropriate jar package with my eclipse (manually plus Jsf-impl.jar)

the directory structure of the example is as follows:

User class:

Import Java.util.Date; public class User {private string name, private string pass; private double; private double; private Date Updat E Public double Getage () {return age;} public void Setage (double age) {this.age = age;} public double Getmoney () {retur n Money; public void Setmoney (double) {This.money = money;} public Date getUpdate () {return update;} public void Setupd Ate (Date update) {this.update = update;} public String GetName () {return name.} public void SetName (string name) {Thi S.name = name; public string Getpass () {return pass;} public void SetPass (String pass) {This.pass = Pass;} public string Checklogin () {if (Name.equals ("Lee") && pass.equals ("Lee") {return "suc";} else{return "Err";} }

message_en.properties, message.properties resource file:

Username=username password=password age=age Money=money update=update javax.faces.component.uiinput.required= Validation error/: {0}value is required.

message_zh_cn.properties resource file:

username=/u7528/u6237/u540d password=/u5bc6/u7801 age=/u5e74/u9f84 money=/u5de5/u8d44 update=/u751F/u65E5 javax.faces.component.uiinput.required=/u4e0d/u80fd/u4e3a/u7a7a

index.jsp:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

login.jsp:

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "H"% > <%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > <f:view> <f:loadbundle basename= "message" var= "MSG"/>

welcome.jsp:

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "H"% > <%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

error.jsp:

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "H"% > <%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Faces-config.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE faces-config Public "-//sun Microsystems, Inc.//dtd JavaServer the faces config 1.1//en" "http://java.sun.com/dtd/ Web-facesconfig_1_1.dtd "> <faces-config> <application> <message-bundle>message</ message-bundle> <locale-config> <default-locale>zh</default-locale> <supported-locale> en</supported-locale> </locale-config> </application> <managed-bean> <managed-bean-name >user</managed-bean-name> <managed-bean-class>com. User</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean > <navigation-rule> <from-view-id>/page/login.jsp</from-view-id> <navigation-case> < From-outcome>suc</from-outcome> <to-view-id>/page/welcome.jsp</to-view-id> </ navigation-case> <navigation-case> <from-outcome>err</from-outcome> <to-view-id>/page/error.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>

Web.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://" Www.w3.org/2001/XMLSchema-instance "version=" 2.4 "xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee http:// Java.sun.com/xml/ns/j2ee/web-app_2_4.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> <welcome-file-list> <welcome-file>index.jsp</ Welcome-file> </welcome-file-list> </web-app>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.