agenda web mvc 2 apache struts
play

Agenda Web MVC-2: Apache Struts Drawbacks with Web Model 1 Web - PowerPoint PPT Presentation

Agenda Web MVC-2: Apache Struts Drawbacks with Web Model 1 Web Model 2 (Web MVC) Rimon Mikhaiel Struts framework rimon@cs.ualberta.ca Example of workflow management. A Hello World Example Web Model 2 Web Model 1 Web MVC


  1. Agenda Web MVC-2: Apache Struts � Drawbacks with Web Model 1 � Web Model 2 (Web MVC) Rimon Mikhaiel � Struts framework rimon@cs.ualberta.ca � Example of workflow management. � A Hello World Example Web Model 2 Web Model 1 Web MVC Model: is responsible for: � Providing the data from the database and saving the data into the data store. � All the business logic are implemented in the Model. � Data entered by the user through View are check in the model before saving � into the database. Data access, Data validation and the data saving logic are part of Model. � � View : is responsible for: Taking the input from the user, � � In a standard J2EE web application: Dispatching the request to the controller, and then � Receiving response from the controller and displaying the result to the user. The client will typically submit information to the server via an HTTP � � HTML, JSPs, Custom Tag Libraries and Resources files are the part of view request. component. The information is then handed over to a Servlet or a JSP which � Controller: is intermediary between Model and View; is responsible for: � processes it, interacts with a database and produces an HTML- Receiving the request from client. � formatted response. Executing the appropriate business logic from the Model, and then � This approaches is often considered inadequate for large projects � Producing the output to the user using the View component. ActionServlet, � because they mix application logic with presentation and make Action, ActionForm and struts-config.xml are the part of Controller. maintenance difficult.

  2. MVC Frameworks Struts Framework � Apache open source web application framework � J2EE: (it is free). � Struts � Base on Model 2 MVC architecture. � Spring MVC � Supports J2EE web application � PHP “ Write once, Run Anywhere ” � � CakePHP � Supports different model implementation (Javabeans, � Strusts4php EJB, etc.) � Extends the Java Servlet API. � C# .NET � Supports different presentation implementation (JSP, � Girders XML/ XSLT, JSF). � Ruby on Rails � Supports internationalization (I18N). � Enable Multi-language applications � Supports application wide standard (error) message. The struts architecture < action-mappings> < action path= "/ login" type= “ com.myproject.action.LoginAction" > Working with Struts < forward name= “ success" path= "/ jsp/ MainMenu.jsp" / > < forward name= “ fail" path= "/ jsp/ LoginView.jsp" / > < / action> http:/ / m y-com pany.com / login.action < / action-mappings> From strut ’ s official download page http://struts.apache.org/download.cgi : � From the recent release, download Struts2-blank.war � Rename Struts2-blank.war to YourProjectName .war (say � MyStruts.war ) Make sure that your tomcat is up and running. � Copy MyStruts.war under ~/catalina/webapps/ the war file will be class LoginForm extends ActionForm � { automatically extract itself under the same directory String username,password; ( ~/catalina/webapps/MyStruts ). public String getUsername(){ return username; Restart your tomcat. � } Test your Struts application through: public void setUsername(String username){ � this.username= username; http: / / machine.cs.ualberta.ca: port/ YourProjectName } (http: / / ui01.cs.ualberta.ca: 17067/ MyStruts) public String getPassword(){ return password; You should see the following screen: � } public void setPassword(String password){ this.password= password; } }

  3. Struts application structure Hello World (JSP page) MyStruts � Create a directory: ~/catalina/webapps/MyStruts/jsps | -- META-INF | -- WEB-INF (jars, classes, and configurations) � Under the above directory, create the following | -- example (jsp files) (optional) HelloWorld.jsp ` -- index.html (front page) (optional) MyStruts/ WEB-INF/ classes < % @ taglib prefix= "s" uri= "/ struts-tags" % > | -- example (package example, you can find the source under ‘ MyStruts/ WEB- INF/ src/ java/ example/ ’ ) < html> | | -- ExampleSupport.class | | -- HelloWorld.class < head> | | -- Login-validation.xml | | -- Login.class < title> Hello World!< / title> | | -- package.properties | ` -- package_es.properties < / head> | -- example.xml ` -- struts.xml < body> < h2> < s: property value= "message" / > < / h2> < / body> < / html> Hello World (Action) Hello World (Mapping) - Create a directory Modify ~/catalina/webapps/MyStruts/WEB-INF/classes/struts.xml � ~/catalina/webapps/MyStruts/WEB- package c410; to look like: import com.opensymphony.xwork2.ActionSupport; INF/src/c410 to store your java public class HelloWorld extends ActionSupport { source code for a package “ c410 ” < !DOCTYPE struts PUBLIC public static final String MESSAGE = "Struts is up and running ..."; "-/ / Apache Software Foundation/ / DTD Struts Configuration 2.0/ / EN" - Create HelloWorld.java under the private String message; above directory. "http: / / struts.apache.org/ dtds/ struts-2.0.dtd"> public String execute() throws Exception { - While being under the above < struts> setMessage(MESSAGE); return SUCCESS; directory, compile source code using: < package name= "c410" extends= "struts-default"> } javac -classpath < action name= "HelloWorld" class= "c410.HelloWorld"> "../../lib/xwork-2.0.4.jar" public void setMessage(String message){ this.message = message; < result> / jsps/ HelloWorld.jsp< / result> HelloWorld.java } < / action> - Create a directory public String getMessage() { ~/catalina/webapps/MyStruts/WEB- < !-- Add your actions here --> return message; INF/classes/c410 and copy the } < / package> } generate HelloWorld.class under this < / struts> directory

  4. Run HelloWorld Exercise for next class � Implement a simple bi-lingual (e.g. English, French, Spanish, � You can run it through etc) login screen: Validate that both user-name and password are given � http://machine.cs.ualberta.ca:p When user-name/ password are given you display a message “ This � page is under construction ” ort/MyStruts/HelloWorld.action When the user clicks on “ Sign On ” , you display the same message � “ This page is under construction ” You should get something like the following screen References � Struts Offical Home Page � Apache Struts2 Documentation: Bootstrap � Struts Guide � What is Struts?

Recommend


More recommend