Struts
Ford Questions and Answers
1) What is programmatic validation how do u implement it?
by extending ValidatorForm or ActionForm and
by overriding validate method in action form
Example:
public class CandidateForm extends ActionForm {
public ActionErrors validate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
ActionErrors errors = new ActionErrors();
if(StringUtils.isNullOrEmpty(candidateName)){
errors.add("candidateName", new ActionMessage("Please enter the
name"));
}
}
2. What are the types of validation in struts?
1) Programmatic
2) Declarative
Declarative Example:
Struts-Config:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn" >
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
/WEB-INF/validator-user.xml"/>
</plug-in>
validator-user.xml
<form-validation>
<formset>
<form name="userForm">
<field property="email" depends="required,email">
<msg name="required" key="err.user.email.required" />
<msg name="email" key="err.user.email.invalid" />
</field> </form> </formset> </form-validation>
3) what is the role of request processor?
The RequestProcessor Class is the actual place where the request processing takes
place in a Struts controller environment.
When the request object first reaches the actionservlet class then it invokes the
process method of the underlying RequestProcessor Class.
This process method then looks into the struts-config.xml file and tries to locate
the name of the action that has come with the request. Once it identifies the action
in the xml file it continues the rest of the steps needed for request processing.
processor has most of the following responsibilities:
Determine path,
Handle Locale,
Process content and encoding type,
Process cache headers
Pre Processing hook
Pre-processing hook,
Determine mapping,
Determine roles,
Process and validate actionForm,
Return a response
4) Can you briefly tell us about the struts flow?
5) Explain action-mapping attributes?
Path - request sent with this name.do
Type - action’s class path
Parameter - (used for dispatchaction) to find the method in action class
Name - form name
Scope - scope of the form bean
Validate – true or false to validate the form
Input - The physical page (or another ActionMapping) to which control should be
forwarded when validation errors exist in the form bean.
Forward - The physical page (or another ActionMapping) to which the control should
be forwarded when the ActionForward with this name is selected in the execute
method of the Action class.
6) Explain lookupdispatch action in detail?
LookupDispatchAction provides a mechanism for grouping a set of related functions into a
single action, thus eliminating the need to create seperate actions for each functions.
Example :
public class UserAction extends LookupDispatchAction {
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("UserForm.add", "add");
map.put("UserForm.update", "update");
map.put("UserForm.delete", "delete");
return map;
}
}
Note: These keys will be replaced by the values in the properties/resourcebundle files
7) What are the tag libraries you have used in struts ?
The six Struts libraries are:
Bean Tags,
HTML Tags,
Logic Tags ,
Nested Tags ,
Template Tags ,
Tiles Tags
struts-bean.tld
struts-html.tld
struts-logic.tld
struts-tiles.tld
8) What are the Framework implemented in Struts?
1) Tiles
2) Validation
9) Do we have struts configuration file struts2.0?
Yes.
10) Why we are going for MVC?
Separation of concerns. Its an design pattern. The general idea is to separate the backend
logic from the frontend that represents the program. Code maintainability.
11)struts.xml vs web.xml?
web.xml is used for the deployment descriptor for web applications where struts-
config.xml is used for deployment descriptor for struts application.
Struts-config.xml is used for making connection between view & controller where as
web.xml is used for making connection between web container & web application
12) Design patterns available within struts framework?
Service to Worker
Dispatcher View
Composite View (Struts Tiles)
Front Controller
View Helper
Synchronizer Token
13) Do you have a generic exception in your application?
Yes. We have.
Specify this in struts file
<global-exceptions>
<exception type="" path="" key="">
</exception>
</global-exceptions>
14) In struts if you need to handle a new request in which files you would have to make the
changes?
1) Struts-config file
2) Jsp changes
3) New action class or add methods to existing action class(if dispatchaction)
4) Validation-rules.xml,validation-XXX.xml
5) Tiles.xml if there is tiles configuration
15) If error comes in ur in validation block, where it will redirect?
input=“/*.jsp”: When using validation in struts “input” attribute in action should be
specified if we want to show errors in same form page.
16) How do you ensure security in struts using struts config file?
<action-mappings>
<action path="/compute" type="com.shiftat.oreilly.web.ComputeAction"
name="computeForm" scope="session" input="/jsp/compute.jsp" className=
"com.shiftat.struts.StrutsPermissionMapping" unknown="false" validate="false">
<set-property property="actionId" value="160" />
<set-property property="label" value="compute"/>
<set-property property="canBeMadeAvailable" value="true"/>
<set-property property="canBeMadeEditable" value="false"/>
<set-property property="group" value="4"/>
<set-property property="role" value="4"/>
<forward name="succes" path="/jsp/result.jsp" redirect="false"/>
</action>
</action-mappings>
Action changes :
if (actionConfig instanceof StrutsPermissionMapping){
StrutsPermissionMapping amp = (StrutsPermissionMapping) actionConfig;
actionMappingMap .put(amp.getActionId(),amp);
} else {
//Regular ActionMapping //without security attributes
}
17) Where do you need request scope and where session?
scope = "session"
means throughout the application ( till the session gets expired). Your can keep the values in
session no matter how many JPSs you are visiting of that application. It remains there in
session until you close your browser.
scope="request"
means for that particular action only you are keeping the values. In this if you call another
action or redirects to another JSP, it gets removed.
18) How to configure message resource in struts?
In struts-config file
<message-resources parameter="ApplicationResources.properties" />
19) Action Form is coming under which part in mvc?
Model.
20) How do you prevent the earlier data from being carried over when user presses the back
button?
1) Hidden variables
21) How many instance of action servlet?
Only one instance can be created.
22) How do you prevent duplicate request submission?
Synchronizer Token. Generate token and save it. Validate that token before
performing any business logic.