JSF: Managed Beans

Document technical information

Format pdf
Size 464.2 kB
First found May 22, 2018

Document content analysis

Category Also themed
Language
English
Type
not defined
Concepts
no text concepts found

Persons

Organizations

Places

Transcript

© 2005 Marty Hall
JSF: Managed Beans
Core Servlets & JSP book: www.coreservlets.com
More Servlets & JSP book: www.moreservlets.com
Servlet/JSP/Struts/JSF Training: courses.coreservlets.com
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.
© 2005 Marty Hall
For live JSF training, please see
JSP/servlet/Struts/JSF training courses at
http://courses.coreservlets.com/.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP, and this tutorial. Available at
public venues, or customized versions can be held
on-site at your organization.
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.
Topics in This Section
• Using beans to represent request
parameters
• Declaring beans in faces-config.xml
• Outputting bean properties
– Standard JSF approach
– JSP 2.0 expression language
• Using properties files
– Standard prompts/strings
– Internationalized text
4
J2EE training and tutorials: http://www.coreservlets.com
Background: What Are Beans?
• Java classes that follow certain conventions
– Must have a zero-argument (empty) constructor
• You can satisfy this requirement either by explicitly
defining such a constructor or by omitting all constructors
– Should have no public instance variables (fields)
• I hope you already follow this practice and use accessor
methods instead of allowing direct access to fields
– Persistent values should be accessed through methods
called getXxx and setXxx
• If class has method getTitle that returns a String, class is
said to have a String property named title
• Boolean properties use isXxx instead of getXxx
– For more on beans, see
http://java.sun.com/beans/docs/
5
J2EE training and tutorials: http://www.coreservlets.com
Why You Should Use Accessors,
Not Public Fields
• To be a bean, you cannot have public fields
• So, you should replace
public double speed;
• with
private double speed;
public double getSpeed() {
return(speed);
}
public void setSpeed(double newSpeed) {
speed = newSpeed;
}
• You should do this in all your Java code
anyhow. Why?
6
J2EE training and tutorials: http://www.coreservlets.com
Why You Should Use
Accessors, Not Public Fields
• You can put constraints on values
public void setSpeed(double newSpeed) {
if (newSpeed < 0) {
sendErrorMessage(...);
newSpeed = Math.abs(newSpeed);
}
speed = newSpeed;
}
– If users of your class accessed the fields directly, then
they would each be responsible for checking constraints.
7
J2EE training and tutorials: http://www.coreservlets.com
Why You Should Use
Accessors, Not Public Fields
• You can change your internal
representation without changing interface
// Now using metric units (kph, not mph)
public void setSpeed(double newSpeed) {
setSpeedInKPH = convert(newSpeed);
}
public void setSpeedInKPH(double newSpeed) {
speedInKPH = newSpeed;
}
8
J2EE training and tutorials: http://www.coreservlets.com
Why You Should Use
Accessors, Not Public Fields
• You can perform arbitrary side effects
public double setSpeed(double newSpeed) {
speed = newSpeed;
updateSpeedometerDisplay();
}
– If users of your class accessed the fields directly, then
they would each be responsible for executing side effects.
Too much work and runs huge risk of having display be
inconsistent from actual values.
9
J2EE training and tutorials: http://www.coreservlets.com
Steps in Using JSF
1. Create a bean to represent the form data
2. Use f:view and h:form to create an input
form
3. Specify the action controller with the
action attribute of h:commandButton
4. Create an action controller that reads the
form data, invokes business logic, stores
results beans, and returns conditions
5. Use faces-config.xml to declare the form
bean and navigation rules
6. Create JSP pages corresponding to each
return condition
•
Access bean properties in these pages
7. Protect raw JSP pages from access
10
J2EE training and tutorials: http://www.coreservlets.com
Example 1: Using Beans
• Similar to previous example
– Original URL will be http://hostname/jsftest/register3.faces
– When form submitted, three possible results
• Error message re illegal email address
• Error message re illegal password
• Success
• Differences
– Action controller obtains request data from bean
– Output pages access bean properties
• Main points
– Creating a bean
– Declaring beans in faces-config.xml
– Outputting bean properties
11
J2EE training and tutorials: http://www.coreservlets.com
Main Points of This Example
• Make bean representing the request parameters
public class MyBean {
public String getBlah() {…}
public void setBlah(String newValue) {…} …
}
• Use faces-config.xml to declare beans
<faces-config>
<managed-bean>
<managed-bean-name>beanName</managed-bean-name>
<managed-bean-class>package.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>…
</faces-config>
• Use h:inputText to associate textfield with property
<h:inputText value="#{beanName.propertyName}"/>
• Use h:outputText to output bean properties
<h:outputText value="#{beanName.propertyName}"/>
12
J2EE training and tutorials: http://www.coreservlets.com
Step 1: Create Form Bean
• One property for each request parameter
– If input form says value="#{name.foo}",then bean
should have getFoo and setFoo methods.
• Additional properties for output values
• Populating bean
– System will fill in property values automatically
– Strings converted as with jsp:setProperty
• Form redisplayed if there are errors: see validation section
• Action controller
13
– Method can access bean properties instead of calling
request.getParameter.
– ExternalContext only needed if you want to access
request headers, cookies, session data, or other HTTPrelated info.
– Method also creates business objects and stores them in
the properties reserved for output
values
J2EE training and tutorials: http://www.coreservlets.com
Step 1: Example Code
• Code for request parameter representation
package coreservlets;
public class RegistrationBean {
private String email = "[email protected]";
private String password = "";
public String getEmail() {
return(email);
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return(password);
}
public void setPassword(String password) {
this.password = password;
}
14
J2EE training and tutorials: http://www.coreservlets.com
Step 1: Example Code
• Code for output values
private SuggestionBean suggestion;
public SuggestionBean getSuggestion() {
return(suggestion);
}
• See www.coreservlets.com for code for
SuggestionBean
– Merely creates a randomized suggested email address and
password
15
J2EE training and tutorials: http://www.coreservlets.com
Step 2: Create Input Form
• Same as previous example, except
– h:inputBlah tags given a value attribute identifying the
corresponding bean property
• Also gives initial value to textfield
– id attribute not needed
• Example code
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>…
<h:form>
Email address:
<h:inputText value="#{registrationBean.email}"/><BR>
Password:
<h:inputSecret value="#{registrationBean.password}"/><BR>
<h:commandButton value="Sign Me Up!"
action="#{registrationBean.register}"/>
</h:form>…
</f:view>
16
J2EE training and tutorials: http://www.coreservlets.com
Step 3: Specify Action
Controller
• Same approach as in previous example
<h:form>
Email address:
<h:inputText value="#{registrationBean.email}"/><BR>
Password:
<h:inputSecret value="#{registrationBean.password}"/><BR>
<h:commandButton value="Sign Me Up!"
action="#{registrationBean.register}"/>
</h:form>
17
J2EE training and tutorials: http://www.coreservlets.com
Steps 2 & 3: Example Results
• File is tomcat_dir/webapps/jsf-test/register3.jsp
• URL is http://localhost/jsf-test/register3.faces
• The [email protected] value comes from the bean
18
J2EE training and tutorials: http://www.coreservlets.com
Step 4: Create an Action
Controller
• Can access the bean properties directly
– System automatically fills them in before calling the
action method
– No need to access ExternalContext or call
request.getParameter
• Fills in properties corresponding to output
values
– These are the results-beans used in traditional MVC
• Still returns strings corresponding to each
of the possible outcomes
– As in previous example
• ExternalContext still often used
– To access cookies, session data, request headers, etc.
19
J2EE training and tutorials: http://www.coreservlets.com
Step 4: Example Code
public class RegistrationBean {
// Properties shown on earlier slides
public String register() {
if ((email == null) ||
(email.trim().length() < 3) ||
(email.indexOf("@") == -1)) {
suggestion = SuggestionUtils.getSuggestionBean();
return("bad-address");
} else if ((password == null) ||
(password.trim().length() < 6)) {
suggestion = SuggestionUtils.getSuggestionBean();
return("bad-password");
} else {
return("success");
}
}
}
20
J2EE training and tutorials: http://www.coreservlets.com
Step 5: Update faces-config.xml
• Declaring bean
…
<faces-config>
<managed-bean>
<managed-bean-name>
registrationBean
</managed-bean-name>
<managed-bean-class>
coreservlets.RegistrationBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
…
</faces-config>
21
J2EE training and tutorials: http://www.coreservlets.com
Step 5: Update faces-config.xml
• Specifying navigation rules
…
<faces-config>
…
<navigation-rule>
<from-view-id>/register3.jsp</from-view-id>
<navigation-case>
<from-outcome>bad-address</from-outcome>
<to-view-id>/WEB-INF/results/bad-address3.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>bad-password</from-outcome>
<to-view-id>/WEB-INF/results/bad-password3.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/WEB-INF/results/result3.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
22
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• Use h:outputText to access
bean properties
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE …>
<HTML>
…
<h:outputText value="#{beanName.propertyName}"/>
…
</HTML>
</f:view>
23
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/bad-address3.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE …>
<HTML>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Illegal Email Address</TH></TR>
</TABLE>
<P>
The address
"<h:outputText value="#{registrationBean.email}"/>"
is not of the form [email protected] (e.g.,
<h:outputText
value="#{registrationBean.suggestion.email}"/>).
<P>
Please <A HREF="register3.faces">try again</A>.
…
</HTML>
</f:view>
24
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Email Address
• Input
25
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Email Address
• Output
26
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/bad-password3.jsp
27
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE …>
<HTML>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Illegal Password</TH></TR>
</TABLE>
<P>
The password
"<h:outputText value="#{registrationBean.password}"/>"
is too short; it must contain at least six characters.
Here is a possible password:
<h:outputText
value="#{registrationBean.suggestion.password}"/>.
<P>
Please <A HREF="register3.faces">try again</A>.
…
</HTML>
</f:view>
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Password
• Input
28
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Password
• Output
29
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/result3.jsp
30
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE …>
<HTML>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Success</TH></TR>
</TABLE>
<H2>You have registered successfully.</H2>
<UL>
<LI>Email Address:
<h:outputText value="#{registrationBean.email}"/>
<LI>Password:
<h:outputText value="#{registrationBean.password}"/>
</UL>
(Version 3)
…
</HTML>
</f:view>
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for
Good Input
• Input
31
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for
Good Input
• Output
32
J2EE training and tutorials: http://www.coreservlets.com
Step 7: Protect Raw JSP Pages
<web-app>
…
<security-constraint>
<display-name>
Prevent access to raw JSP pages that are for JSF pages.
</display-name>
<web-resource-collection>
<web-resource-name>Raw-JSF-JSP-Pages</web-resource-name>
<!-- Add url-pattern for EACH raw JSP page -->
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/register1.jsp</url-pattern>
<url-pattern>/register2.jsp</url-pattern>
<url-pattern>/register3.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>No roles, so no direct access</description>
</auth-constraint>
</security-constraint>
</web-app>
33
J2EE training and tutorials: http://www.coreservlets.com
Alternative Approaches
• Preview: using the JSP 2.0 EL
– If output pages only display bean properties (rather than
manipulating a form or using form elements):
• Why bother with f:view and associated taglib declaration?
• Why use h:outputText and associated taglib declaration
when the JSP 2.0 EL is simpler?
– If you use the JSP 2.0 EL, you must:
• Be in a JSP 2.0 container (e.g., Tomcat 5, not Tomcat 4)
• Use the JSP 2.0 declaration for web.xml (see later section)
• Separating request parameters from action
controller
– If you use two separate classes, how does controller class
get access to bean that represents request params?
• Get ExternalContext and call getSessionMap
• Wire beans together in faces-config.xml
34
J2EE training and tutorials: http://www.coreservlets.com
Using the JSP 2.0 Expression
Language
• Standard JSF approach
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE …>
<HTML>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Success</TH></TR>
</TABLE>
<H2>You have registered successfully.</H2>
<UL>
<LI>Email Address:
<h:outputText value="#{registrationBean.email}"/>
<LI>Password:
<h:outputText value="#{registrationBean.password}"/>
</UL>
(Version 3)
…
</HTML>
</f:view>
35
J2EE training and tutorials: http://www.coreservlets.com
Using the JSP 2.0 Expression
Language
• JSP 2.0 approach
– Omit taglib declarations and f:view tags
– Shorten expression that outputs bean properties
<!DOCTYPE …>
<HTML>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Success</TH></TR>
</TABLE>
<H2>You have registered successfully.</H2>
<UL>
<LI>Email Address: ${registrationBean.email}
<LI>Password: ${registrationBean.password}
</UL>
(Version 3)
…
</HTML>
36
J2EE training and tutorials: http://www.coreservlets.com
Example 2: Properties Files
• This example is very similar to the previous one.
– However, some of the standard prompts and messages are taken
from a properties file instead of being hardcoded in the JSP pages.
• Steps
– Load a properties file with f:loadBundle
– Output values with #{bundleName.propertyName}
• Advantages to using messages from properties
files
– If a message is used in several places, it can be updated with a single
change. This is consistent with the JSF philosophy of making as
many changes as possible in config files, not in Java or JSP code.
– If you use messages pervasively in your JSP pages, you can
internationalize your application by having multiple properties files
corresponding to the locale (e.g., application.properties,
application_es.properties, applications_jp.properties, etc., as with
standard I18N in Java).
37
J2EE training and tutorials: http://www.coreservlets.com
Main Points of This Example
• Put standard messages in a normal Java
properties file under WEB-INF/classes
– Note that names should not contain dots as in Struts
name1=value1
name2=value2
…
• Load the properties file with f:loadBundle
– Suppose the properties file is
WEB-INF/classes/someDir/foo.properties
– Load it with the following
<f:loadBundle basename="someDir.foo" var="someName"/>
– Notice that you do not list WEB-INF/classes or the file
extension
• You still use h:outputText
38
<h:outputText value="#{someName.propertyName}"/>
J2EE training and tutorials: http://www.coreservlets.com
Properties File for This
Example
• WEB-INF/classes/coreservlets/prompts.properties
formTitle=New Account Registration
emailPrompt=Email Address
passwordPrompt=Password
buttonLabel=Sign Me Up!
emailError=Illegal Email Address
passwordError=Illegal Password
passwordLength=six
39
J2EE training and tutorials: http://www.coreservlets.com
Step 1: Create Form Bean
• Same bean as in previous example
40
J2EE training and tutorials: http://www.coreservlets.com
Step 2: Create Input Form
• Same as previous example, except title
and prompts taken from properties file
– Actual file is
WEB-INF/classes/coreservlets/prompts.properties
– Remember you have to do h:loadBundle in
each file
• Unlike in Struts
• On the other hand, Struts requires you to declare
properties file in the config file, and JSF does not
41
J2EE training and tutorials: http://www.coreservlets.com
Step 2: Example Code
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<f:loadBundle basename="coreservlets.prompts" var="prompts"/>
…
<TITLE><h:outputText value="#{prompts.formTitle}"/></TITLE>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
<h:outputText value="#{prompts.formTitle}"/></TH></TR>
</TABLE>
<P>
<h:form>
<h:outputText value="#{prompts.emailPrompt}"/>:
<h:inputText value="#{registrationBean.email}"/><BR>
<h:outputText value="#{prompts.passwordPrompt}"/>:
<h:inputSecret value="#{registrationBean.password}"/><BR>
<h:commandButton value="#{prompts.buttonLabel}"
action="#{registrationBean.register}"/>
</h:form>
</CENTER></BODY></HTML>
</f:view>
42
J2EE training and tutorials: http://www.coreservlets.com
Step 3: Specify Action
Controller
• Exactly the same as in previous example
<h:form>
<h:outputText value="#{prompts.emailPrompt}"/>:
<h:inputText value="#{registrationBean.email}"/><BR>
<h:outputText value="#{prompts.passwordPrompt}"/>:
<h:inputSecret
value="#{registrationBean.password}"/><BR>
<h:commandButton value="#{prompts.buttonLabel}"
action="#{registrationBean.register}"/>
</h:form>
43
J2EE training and tutorials: http://www.coreservlets.com
Steps 2 & 3: Example Results
• File is tomcat_dir/webapps/jsf-test/register4.jsp
• URL is http://localhost/jsf-test/register4.faces
• The [email protected] value comes from the bean
44
J2EE training and tutorials: http://www.coreservlets.com
Step 4: Create an Action
Controller
• Same as before
– The same bean is used in this example and the previous
one.
– This illustrates the advantages of both JSF and Struts over
standard MVC: you can reuse the same controller but
map return conditions to different output pages
45
J2EE training and tutorials: http://www.coreservlets.com
Step 5: Update faces-config.xml
• Declaring bean already done
– No need to repeat previous declaration
…
<faces-config>
<managed-bean>
<managed-bean-name>
registrationBean
</managed-bean-name>
<managed-bean-class>
coreservlets.RegistrationBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
…
</faces-config>
46
J2EE training and tutorials: http://www.coreservlets.com
Step 5: Update faces-config.xml
• Specifying navigation rules
– You do need a new entry for this
…
<faces-config>
…
<navigation-rule>
<from-view-id>/register4.jsp</from-view-id>
<navigation-case>
<from-outcome>bad-address</from-outcome>
<to-view-id>/WEB-INF/results/bad-address4.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>bad-password</from-outcome>
<to-view-id>/WEB-INF/results/bad-password4.jsp
</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/WEB-INF/results/result4.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
47
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/bad-address4.jsp
48
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<f:loadBundle basename="coreservlets.prompts" var="prompts"/>
…
<TITLE><h:outputText value="#{prompts.emailError}"/></TITLE>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
<h:outputText value="#{prompts.emailError}"/></TH></TR>
</TABLE>
<P>
The address
"<h:outputText value="#{registrationBean.email}"/>"
is not of the form [email protected] (e.g.,
<h:outputText
value="#{registrationBean.suggestion.email}"/>).
<P>
Please <A HREF="register4.faces">try again</A>.
…
</f:view>
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Email Address
• Input
49
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Email Address
• Output
50
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/bad-password4.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<f:loadBundle basename="coreservlets.prompts" var="prompts"/>
…
<TITLE><h:outputText value="#{prompts.passwordError}"/></TITLE>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
<h:outputText value="#{prompts.passwordError}"/></TH></TR>
</TABLE>
<P>
The password
"<h:outputText value="#{registrationBean.password}"/>"
is too short; it must contain at least
<h:outputText value="#{prompts.passwordLength}"/> characters.
Here is a possible password:
<h:outputText value="#{registrationBean.suggestion.password}"/>.
<P>
Please <A HREF="register3.faces">try again</A>.
…</f:view>
51
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Password
• Input
52
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for Bad
Password
• Output
53
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Create Output JSP
Pages
• …/jsf-test/WEB-INF/results/result4.jsp
54
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<f:loadBundle basename="coreservlets.prompts" var="prompts"/>
…
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Success</TH></TR>
</TABLE>
<H2>You have registered successfully.</H2>
<UL>
<LI><h:outputText value="#{prompts.emailPrompt}"/>:
<h:outputText value="#{registrationBean.email}"/>
<LI><h:outputText value="#{prompts.passwordPrompt}"/>:
<h:outputText value="#{registrationBean.password}"/>
</UL>
(Version 4)
…
</f:view>
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for
Good Input
• Input
55
J2EE training and tutorials: http://www.coreservlets.com
Step 6: Example Result for
Good Input
• Output
56
J2EE training and tutorials: http://www.coreservlets.com
Step 7: Protect Raw JSP Pages
<web-app>
…
<security-constraint>
…
<web-resource-collection>
<web-resource-name>Raw-JSF-JSP-Pages</web-resource-name>
<!-- Add url-pattern for EACH raw JSP page -->
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/register1.jsp</url-pattern>
<url-pattern>/register2.jsp</url-pattern>
<url-pattern>/register3.jsp</url-pattern>
<url-pattern>/register4.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>No roles, so no direct access</description>
</auth-constraint>
</security-constraint>
</web-app>
57
J2EE training and tutorials: http://www.coreservlets.com
Summary
• Create a bean
– Properties for each request parameter
– Action controller method
– Properties to hold results objects
• Declare bean in faces-config.xml
– Use managed-beans declaration
• Use h:outputText to output bean properties
– The JSP 2.0 expression language is often better, however
• If you have standard prompts (or
internationalized text)
– Put prompts/text in properties file
– Load file with f:loadBundle
– Output properties with h:outputText
58
J2EE training and tutorials: http://www.coreservlets.com
© 2005 Marty Hall
Questions?
Core Servlets & JSP book: www.coreservlets.com
More Servlets & JSP book: www.moreservlets.com
Servlet/JSP/Struts/JSF Training: courses.coreservlets.com
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.

Similar documents

×

Report this document