Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
action(*.do
)
ActionServl
Server
After the server has created servlet object server executed init()
method. As part of the init() method it with find the configuration file
name details and by using the parser program. It read the contents
from configuration file and stores it in JVMs memory.
action(*.do)
init()
{
}
struts-config.xml
ActionServlet
Server
}
public void destroy(){
//code to remove the contents from JVMs memory.
}
}
When ever any client set the request to the server whose URL pattern
and with .do ActionServlet service() method throws an Exception
saying Invalid path Exception.
A frame work defined a procedure to carry out a task/work.
We get the path of the resource from the URL.
/one.do
/one
/some/thing.do
/some/thing
/
any/thing/work.d
URL
/any/thing/work
path of the Resources
ActionOne
ActionTwo
// struts-config.xml
When ever we deploy a project, server reads the content from web.xml
server represents every tag in the form of object. When we deploy
struts project, servers has created the following two objects.
name =
name =
actionclass
=
ActionServl
action
url pattern =
*.do
et
ServletMapping
tag object
When ever server has encountered an action tag in
configuration file, it is mapped with an action mapping object.
struts
type =
<action type = ActionOne path = /a0/> ActionOn
e path =
/a0
ActionMapping object
The following steps will be carried out when a client has send the
request to server where URL pattern and with .do.
1. Server creates request and response objects and hand over then
to ActionServlet service() method.
2. ActionServlet service() method remove .do. Now we get the path
of the resource.
3. Service() metod is these any action mapping object is availability
whose path matches to /a0.
4. If there is no actionmapping object available server throws an
Exception invalid path Exception.
5. If the actionmapping object is available server gets the name of
the action class. If require action servlet create action class object
and call the execute() method.
6. Execute() method returns on ActionForward object with null value.
7. When ever ActinServlet service() has encoder a null value stops
the execute of request. It will display a blank screen to the user.
Service(
){
/a0
2
Request 1
.do
/a0.do
Client
req
ActionServlet
3
4
res
execute
(){
af
}
ActionOne
JEE Server
}
}
When ever we change struts configuration file we must restart the
server. This is because, ActionServlet service() method reads the
contents from struts configuration file at the time of deployment only.
Parameters: We use Parameter attribute in the configuration file to
supply on extra information to that particular action.
Type: use to specify ActionClass name.
Name: Name attribute is used as a logical name to identify a
particular action this name attribute is used mainly in developing form
based applications.
ActionForward:
As part of a struts configuration file, we have added an extra
tag <forward>
<action-mapping>
<action type = ActionThree path = /ath>
<forward name = one path = one.jsp/>
<forward name = two path = two.jsp/>
</action>
</action-mapping> //struts-config.xml
When we deploy the project for the above configuration server creates
ActionMapping objects based <action>tag, and ActionForward objects
based on <forward> tag.
name = one
path = /one.jsp
type =
ActionType
path = /ath
ActionForwards
name = two
type =
ActionOne
path = /a0
path = /two.jsp
The following steps are carried and when the client sends the request
to server.
1. ActionServlet service() method Action class object if requirement.
Request
Client
/ath.do
res
af
}
ActionServlet
On
e.j
sp
execute(
){
af(actio
nforwar
ActionThree
d)}
JEE Server
Develop a struts based application to retrieve all the records from emp
table and display to the client. We should not create any java code in
jsps.
1. Develop a supportive class to store employee details.
public class EMP{
String eno;
String name;
String salary;
3. The following is the jsp which display output to the client by using
core tag library.
<%@ page isELIgnored = false%>
<%@ Laglib uri = http://java:sun.com/jsp/jstl/core prefix =
c%>
<c:forEach var = emp items = ${empList}>
${emp.eno}
${emp.name}
${emp.salary}<br>
</c:forEach>
// search.jsp
*JSTL guys has given SQL tag library. This tag library contains set of
tags which re used to interact with Database server. Its not
recommended to use SQL tag library the jsps this is because if we use
SQL tag library we are c lab in Business logic and presentation logic.
The following data source and update queries are used to write jdbc
code and the jsps.
<%@ page isELIgnored = false%>
<%@ taglib uri = http://java.sun.com/jsp/jstl/sql prefix = sql%>
<sql:setDataSource
var = ds
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@localhost:1521:xe
user = system
password = malli/>
<sql:update dataSource = ${ds}>
insert into emp values(6,esix,454)
</sql:update>
*The following query tag is used to display all the records from emp
table.
<sql:query var = rs dataSource = ${ds}>
select * from emp
</sql:query/>
<c:forEach var = row items = ${rs.rows}>
${row.eno}
${row.name}
${row.salary}
</c:forEach>
I18N application:
1. The application which display the output the client based on they
regional language.
2. We can use property files to develop I18N applications as well as
web based applications.
3. By using property files we can remove hard coding also.
4. Every property file must end with an extension called as
properties. In a project we maintain multiple property files.
5. In a property file we supply the data in the form of key and value.
Ex:
Key1 =
value1
Key2 =
// test.properties
// db.properties
public class DBConnect{
public static void main(String args[]) throws Exception{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(db properties);
prop.load(fis);
System.out.println(prop.getProperty(drv));
System.out.println(prop.getProperty(url));
System.out.println(prop.getProperty(uname));
System.out.println(prop.getProperty(pwd));
}
}
The following java program write the contains to a property file.
public class MyApp{
public static void main(String args[])throws Exception{
Properties prop = new Properties();
prop.setProperty(one,one value);
prop.setProperty(two,two value);
FileOutputStream fos = new FileOutputStream(abc.properties);
prop.store(fos,null);
}
}
We can write the comments in a property file we can use a symbol #.
We can read the
getResourceAsStream.
contents
from
property
file
by
using
//db.properties
The following java program writes the properties into property file.
public class MyApp{
public static void main(String args[]){
Property p = new Property();
p.setProperty(one,onevalue);
p.setProperty(two,twovalue);
FileOutputStream fos = new FileOutputStream(abc.properties);
p.store(fos,null);
}
}
We can write the comments in a property file we can simply use #.
We can read the context
getResourceAsStream().
from
property
file
by
using
To find the locate information of our computer use have to get the
locate object from the locate object. We get country code and
language code.
public class MyApp{
public static void main(String args){
Locate l = Locate.getDefault();
System.out.println(l.getCountry());
System.out.println(l.getLanguage());
}
}
To develop I18N applications we need to create multiple property files.
These property files are called as Bundle.
We create these property files based on number of languages that has
to be supported by the customer.
Procedure to use I18N applications in standalone application:
1. We need to create property files. The numbers of property files
are based on the languages are supported by our projects. Make
sure that Bundle name must be same.
2. In our project we would like to support two languages English and
French.
msg = welcome to java
// MyProject_en_US.properties
// MyProject_fr_CA.Properties
ResourceBoundle rb = ResourceBoundle.getBoundle(MyProject,l);
String m = rb.getString(msg);
System.out.println(m);
}
}
When we are retrieving a key from the ResourceBoundle which is not
available it thrown an Exception java.util.MissingResourceException.
When we test our application for the property files which doesnt
support
other
languages
application
throws
an
Exception
java.util.MissingResourceException.
The project supports two languages and when you run the same
project in a computer which doesnt support above two languages and
we want to display the default language. We achieve this from the
property files we have to remove country code and language code.
Developing I18N applications for web-based:
1. We have developed the following JSP to display the o/p based on
client regional language. But it always displayed o/p in English
language. This is because we have hard coded the values in JSP.
2. From the following JSP we have to remove the high located hard
code values.
<html>
<head>
<tittle>welcome to my project</tittle>
</head>
<body>
<h1><center>welcome to my project</center>m/h1>
<p>Firstline</p>
<p>Secondline</p>
<p>Thirdline</p>
</body>
</html>
When the client sends the request to Server, Server creates request
and Response Object. Server fined the client Regional language and
creates a Locate Object. The Locate Object is associated with request
object.
re
q
en
US
re
s
Accept-Language (en-US)
Client1
Accept-Language (fr-CA)
re
q
fr
CA
re
s
Client2
Server
The following JSP demonstrate how do we find the Locate Object of
client and send it back.
<%@ page import = java.util.*%>
<%
Locate l = requet.getLocate();
out.println(l);
%> //two.jsp
1. Create the property files to support multiple languages.
Ex:
//Message_en_US.Properties
Copy all the property files into classs folder. This is because the
entire web-based application class path is set to classs folder.
//one.JSP
The disadvantage of above the JSP is we are providing lot of java code
its not recommended to write java code in JSPs. to remove that java
code we use I18N tag library.
Req: Develop one.JSP by using struts I18N application.
Procedure to use I18N application in struts.
1. Create the property files and copy them in to classes folder.
2. Configure property files into struts configuration file by using
<MessageResources parameter = />
//tag
Ex:
<struts-config>
------------------<Message-resources parameter = welcome/>
</struts-config>
3.
// struts-config.xml
Import the tag lib in JSP and use message tag of Bean tag
library (strutsTaglib = Bean, HTML, Nested, Logic)
<%@ taglib uri = http://struts.apache.org/tags-bean prefix =
bean%>
<bean:Message key = msg.tittle/><br>
<bean:Message key = msg.header/><br>
<bean:Message key = msg.l1/><br>
<bean:Message key = msg.l2/><br>
<bean:Message key = msg.l3/><br>
// One.jsp
When we use the message <bean:message> tag for a key which is not
available it will throw an Exception saying missing message key key
name for Bundle.
Generally when multiple people are working in same project and they
want to share the property files and when ever we are using a key
which is not available my application should not display any Exception
to achieve. This we have to configure null = false in the struts-configxml.
<MessageResources parameter = welcome null = false/>
By default struts uses null = false the meaning of null = true is if
the key is not available it returns null to the struts. When it return a
null value to strut it throws an Exception.
When we specify null = false, even if the key is not available it will
not thrown an Exception it throws the following Error.
???en-US.msg.l5??? // country and language of Browsers.
Req Assign:
eno name salary
resone_en_US.Properties
One = One in French
resone_en_US.Properties
four = One in French
resone_fr_CA.Properties
resone_fr_CA.Properties
Basename = resone
Basename = restwo.
When ever the project is deployed server creates the servlet context
object. Immediately server calls the init() method. In this method have
the code to read the data from strut-configuration file. When ever the
server found the tag it will create the objects to each tag. Now the
server fined <message_resources > tag. Now the server creates
the object to <message_resource>. Because this class is provide the
implementation to the <message_resources>.
Now server get the parameter value from the
<message-resource>. Here i.e parameter = resource. Now the server
checks into classes folder is there any bundle name like resone. If it is
available the server reads the property files and store it in pmr1
object.
Now server store that pmr1 object in ServletContext object. Here the
server checks whenever the <message-resource> tag contains any
key attribute. If it is not available it takes default key as
org.apache.struts.Action.Message. If it is available it takes key and
store in context object [see preview page].
When we configure resource bundles with out
key they try to store two bundles into application scope by using same
key org.apache.Struts.Action.Message. When ever server has
encountered <message-resource> tag server creating an object to a
class whose name is PropertyMessageResource.
<message-resources parameter = resone/>
<message-resources parameter = restwo/>
When the above configuration is Exception it will try to create two
pmr objects and trying to use the same default key.
en
fr
US
CA
MESSAGE
pmr2
pmr1
Application
resone
restwo
pmr1
pmr2
Sapp
en
fr
US
CA
Server
When we modify the property files we must restart the server.
org.apache.Struts.action.MESSAGE key available in a class called as
Gobles [MESSAGE-KEY]. The logic tag library contains set of tags which
are used to check the logic contains.
Ex: present, not present, grater than, less than and etc.
To check whether the application scope contains the following key.
org.apache.Struts.ActionMESSAGE.
<%@ tag lib uri = http://strus.apache.org/tags-logic prefix =
logic%>
<logic:Present name = org.apache.struts.action.MESSAGE Scope =
application>
Available
</logic:Present>
How to deal with Errors in Struts:
1. In struts 1.3 they are given three different classes to deal with
Errors/Messages: They are action.
1.1. ActionErrors
1.2. ActionMessage
1.3.
ActionMessages
ActionMessages
if Error
if normal message
ActionMessages
//Struts-Config.xml
//One.jsp