Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Web applications with JavaServer Faces

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use JavaServer Faces to develop Web applications related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to use JavaServer Faces to develop Web applications will have a harvest, let's take a look at it.

1. Event handling.

XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >

The next step is to write an event handler to respond to component events (such as selecting a multi-selected option or clicking a button to submit a form, etc.). For simple applications, you also need to indicate which page will be accessed when a form is submitted or a hyperlink is accessed. You can implement the ApplicationHandler interface to achieve this goal. The following code snippet shows an example. In this example, I want to see if FormEvent is triggered by the Submit button in index.JSP. If so, the component tree ID is set to the ID of the component tree associated with the index.jsp page.

Code 5: BasicApplicationHandler.Java

Import java.util.SortedMap

Import javax.faces.FacesException

Import javax.faces.component.UIComponent

Import javax.faces.context.FaceSCOntext

Import javax.faces.tree.Tree

Import javax.faces.tree.TreeFactory

Import javax.faces.FactoryFinder

Import javax.faces.lifecycle.ApplicationHandler

Import javax.faces.event.FormEvent

Import javax.faces.event.FacesEvent

Import javax.faces.event.CommandEvent

Import com.sun.faces.RIConstants

Public class BasicApplicationHandler implements ApplicationHandler {

Public boolean processEvent (FacesContext context, FacesEvent facesEvent) {

If (! (facesEvent instanceof FormEvent) & &

! (facesEvent instanceof CommandEvent)) {

Return true

}

Boolean returnValue = false

String treeId = null

If (facesEvent instanceof FormEvent) {

FormEvent formEvent = (FormEvent) facesEvent

If (formEvent.getCommandName () .equals ("submit")) {

TreeId = "/ hello.jsp"

}

ReturnValue = true

} else if (facesEvent instanceof CommandEvent) {

CommandEvent commandEvent = (CommandEvent) facesEvent

UIComponent c = commandEvent.getComponent ()

If (c.getAttribute ("target")! = null) {

TreeId = (String) c.getAttribute ("target")

ReturnValue = true

}

}

If (null! = treeId) {

TreeFactory treeFactory = (TreeFactory)

FactoryFinder.getFactory (FactoryFinder.TREE_FACTORY)

Context.setTree (treeFactory.getTree (context,treeId))

}

Return returnValue

}

}

2. Develop context listener.

If you take a closer look at the deployment description file web.xml, you will notice that I declared a servlet context listener (BasicServletContextListener). When the application starts, the servlet container creates an instance of servlet context listener and calls its contextInitialized method; when the application is closed, its contextDestroyed method is called. Here is an example of servlet context listener.

Code 6: BasicServletContextListener.java

Import javax.servlet.ServletContextListener

Import javax.servlet.ServletContextEvent

Import javax.faces.FactoryFinder

Import javax.faces.lifecycle.LifecycleFactory

Import javax.faces.lifecycle.Lifecycle

Import javax.faces.lifecycle.ApplicationHandler

Public class BasicServletContextListener implements ServletContextListener {

Public BasicServletContextListener () {

}

Public void contextInitialized (ServletContextEvent e) {

ApplicationHandler handler = new BasicApplicationHandler ()

LifecycleFactory factory = (LifecycleFactory)

FactoryFinder.getFactory (FactoryFinder.LIFECYCLE_FACTORY)

Lifecycle lifecycle =

Factory.getLifecycle (LifecycleFactory.DEFAULT_LIFECYCLE)

Lifecycle.setApplicationHandler (handler)

}

Public void contextDestroyed (ServletContextEvent e) {

}

}

3. Complete the response page.

When the form in index.jsp is submitted, the application processor starts, and the user is forwarded to the response page hello.jsp. The code is as follows:

Code Sample 7: hello.jsp

Hello

This is the end of the article on "how to develop Web applications with JavaServer Faces". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to develop Web applications with JavaServer Faces". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report