Struts and SWT
from
JoeUser Forums
Well! I am learning Struts and SWT these days.
I tried to integrate Struts with SWT... It's simple! The only problem was that nothing was available on net
or may be I couldn't find anything, and I had to workout things myself.
I am not sure if this is the right place to put a technical document like this or not, but this being my second article, I guess I need some more confidence before I place it on any tech. site.
Prerequisite: Basic knowledge of Struts and SWT
In this article
This is a very simple example and shows only the basic integration. Here we will create a simple form to take user input for Username, Password and Password Check field.
In next article:
We will verify that the Username is not blank, has more than 5 character and less than 15 characters.
Then we will verify that the password and password check field are same.
After successful validation the user is shown a successful logon page else he is shown an appropriate error.
All of us who have worked with Struts and JSP, this is simple ...but when I had to integrate Struts with SWT, I had to do little bit of brain-storming.
So here we go....
I have created all the files in strutsTutorial package
Create bean for fields:
Our first step is to create a bean for all the required fields
package strutsTutorial;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class EntryForm extends ValidatorForm {
// Creating our private variables
private String userName;
private String password;
private String passwordChk;
// Getter and setter methods.
public String getUserName() {
return userName;
}
public void setUserName(String string) {
userName = string;
}....................
Create SWT Screen :
Now we need to create an SWT form for the user input..
This is our SWT Screen.
Create Action:
........
public class EntryAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String client = (String) request.getHeader("user-agent");
// Forward control to the success page
if (client.equals("SUCCESS"))
return (mapping.findForward("success"));
else
return (mapping.findForward("failure"));
}
}
Create JSP Files:
Here we can create 2 JSP files. First one showing that user has successfully registered and another one showing any error during registration.
Errors can be displayed using html:errors tag in errors.jsp
Entries Required in Struts-Config.xml:
First we need to make the form-bean entry:
form-bean name="EntryForm" type="strutsTutorial.EntryForm"
The form-bean name "EntryForm" relates to "strutsTutorial.EntryForm"
Then we need to make the action path entry
action path="/entryScreen" type="strutsTutorial.EntryAction" name="EntryForm"
forward name="success" path="/pages/success.jsp"
This entry specifies that in case of success redirect to success.jsp.
Submit Action:
In the SWT form we need to specify what should happen when the form is submitted:
submit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
/ / specify the url.
URL url =
new URL("http://localhost:9080/StrutsProj/entryScreen.do");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("user-agent", "SUCCESS");
// setting the values for bean fields
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(conn.getOutputStream()));
out.write(
"userName="
+ userNameText.getText()
+ "&password="
+ passwordText.getText()
+ "&passwordChk="
+ passwordChkText.getText());
out.flush();
out.close();
String c = conn.getHeaderField("Set-Cookie");
BufferedReader in =
new BufferedReader( new InputStreamReader(conn.getInputStream()));
String line;
// printing the jsp page
while ((line = in.readLine()) != null) {
System.out.println(line);
}
The username, password and passwordChk will map to the bean fields.
Once this action is attached to the Submit button, success.jsp page should be printed on the console and the bean values should be set.Since we have not added any validations hence, we are not able to validate the values entered. In my next article I shall cover how to add validations.
I tried to integrate Struts with SWT... It's simple! The only problem was that nothing was available on net
I am not sure if this is the right place to put a technical document like this or not, but this being my second article, I guess I need some more confidence before I place it on any tech. site.
Prerequisite: Basic knowledge of Struts and SWT
In this article
This is a very simple example and shows only the basic integration. Here we will create a simple form to take user input for Username, Password and Password Check field.
In next article:
We will verify that the Username is not blank, has more than 5 character and less than 15 characters.
Then we will verify that the password and password check field are same.
After successful validation the user is shown a successful logon page else he is shown an appropriate error.
All of us who have worked with Struts and JSP, this is simple ...but when I had to integrate Struts with SWT, I had to do little bit of brain-storming.
So here we go....
I have created all the files in strutsTutorial package
Create bean for fields:
Our first step is to create a bean for all the required fields
package strutsTutorial;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class EntryForm extends ValidatorForm {
// Creating our private variables
private String userName;
private String password;
private String passwordChk;
// Getter and setter methods.
public String getUserName() {
return userName;
}
public void setUserName(String string) {
userName = string;
}....................
Create SWT Screen :
Now we need to create an SWT form for the user input..
This is our SWT Screen.
Create Action:
........
public class EntryAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String client = (String) request.getHeader("user-agent");
// Forward control to the success page
if (client.equals("SUCCESS"))
return (mapping.findForward("success"));
else
return (mapping.findForward("failure"));
}
}
Create JSP Files:
Here we can create 2 JSP files. First one showing that user has successfully registered and another one showing any error during registration.
Errors can be displayed using html:errors tag in errors.jsp
Entries Required in Struts-Config.xml:
First we need to make the form-bean entry:
form-bean name="EntryForm" type="strutsTutorial.EntryForm"
The form-bean name "EntryForm" relates to "strutsTutorial.EntryForm"
Then we need to make the action path entry
action path="/entryScreen" type="strutsTutorial.EntryAction" name="EntryForm"
forward name="success" path="/pages/success.jsp"
This entry specifies that in case of success redirect to success.jsp.
Submit Action:
In the SWT form we need to specify what should happen when the form is submitted:
submit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
/ / specify the url.
URL url =
new URL("http://localhost:9080/StrutsProj/entryScreen.do");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("user-agent", "SUCCESS");
// setting the values for bean fields
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(conn.getOutputStream()));
out.write(
"userName="
+ userNameText.getText()
+ "&password="
+ passwordText.getText()
+ "&passwordChk="
+ passwordChkText.getText());
out.flush();
out.close();
String c = conn.getHeaderField("Set-Cookie");
BufferedReader in =
new BufferedReader( new InputStreamReader(conn.getInputStream()));
String line;
// printing the jsp page
while ((line = in.readLine()) != null) {
System.out.println(line);
}
The username, password and passwordChk will map to the bean fields.
Once this action is attached to the Submit button, success.jsp page should be printed on the console and the bean values should be set.Since we have not added any validations hence, we are not able to validate the values entered. In my next article I shall cover how to add validations.