Struts 2
including Action, Service class including accessing input parameters, use of Value
Stack.
Struts.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE struts
PUBLIC
"-//Apache Software
Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include
file="struts-default.xml"/>
<package
name="a" namespace="/tutorials"
extends="struts-default">
<action name="getTutorials"
class="com.javaetutorials.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>/
TutorialAction.java(Action
class)
package
com.javaetutorials.action;
import
com.javaetutorials.service.TutorialFinderService;
public class TutorialAction
{
/*Interceptors
will pick the query parameter and save it by default.Accessing the input
parameters*/
private String language;
private String bestTutorialSite;
public String
getBestTutorialSite() {
return bestTutorialSite;
}
public void
setBestTutorialSite(String bestTutorialSite) {
this.bestTutorialSite = bestTutorialSite;
}
public String
getLanguage() {
return language;
}
public void
setLanguage(String language) {
this.language = language;
}
public String
execute() {
TutorialFinderService
tf = new
TutorialFinderService();
/* bestTutorialSite must be outside the method so that it can be accessed in
JSP using struts-tag lib */
setBestTutorialSite(tf.getBestTutorialSite(getLanguage()));
return "success";
}
}
TutorialFinder.java (Service Class)
package
com.javaetutorials.service;
public class TutorialFinderService
{
public String
getBestTutorialSite(String
language) {
if(language.equals("java"))
return "javaetutorials";
else
return "Language
not supported";
}
}*
Success.jsp
<%@ page
language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix
="s"
uri="/struts-tags"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Struts Example</title>
</head>
<body>
Business Service Executed...
<br>
<s:property value="bestTutorialSite"/> <br>
<s:property value="language"/>
</body>
</html>
Outputs :
No comments:
Post a Comment