Java technológiák 7. előadás A Struts keretrendszer



Similar documents
Szervletek. ANTAL Margit. Sapientia - EMTE, Pannon Forrás,,Egységes erdélyi felnőttképzés a

DTS Web Developers Guide

Tutorial: Building a Web Application with Struts

Struts Tools Tutorial. Version: M5

Developing Java Web Applications with Jakarta Struts Framework

Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

MVC pattern in java web programming

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

2. Follow the installation directions and install the server on ccc

WEB APPLICATION DEVELOPMENT. UNIT I J2EE Platform 9

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

Java Server Pages and Java Beans

Course Name: Course in JSP Course Code: P5

Web Application Development Using Borland JBuilder 8 WebLogic Edition

This tutorial gives you an overview about the StrutsTestCases extension of JUnit and example how you can use it to test your Struts application.

SSC - Web development Model-View-Controller for Java web application development

Web Container Components Servlet JSP Tag Libraries

2.8. Session management

Portals, Portlets & Liferay Platform

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Design and Implementation of J2EE-based Online Examination System Yingjie Fu

Application Security

<?php if (Login::isLogged(Login::$_login_front)) { Helper::redirect(Login::$_dashboard_front); }

Client-server 3-tier N-tier

An Architecture for Web-based DSS

AJAX and JSON Lessons Learned. Jim Riecken, Senior Software Engineer, Blackboard Inc.

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

Complete Java Web Development

Code Reviewing Web App Framework Based Applications By Abraham Kang, Principal Security Researcher, Software Security Research, HP

AW Applications egység

Java Appletek II. Applet GUI

Bevezetés a C++ Standard Template Library-be

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

In this chapter, we lay the foundation for all our further discussions. We start

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

Open Access Design and Implementation of Online Shopping System Based on the Struts Framework. Zhang Li 1,* and Zhao Luhua 2

JWIG Yet Another Framework for Maintainable and Secure Web Applications

Forms, CGI Objectives. HTML forms. Form example. Form example...

Hello World RESTful web service tutorial

CrownPeak Java Web Hosting. Version 0.20

A Guide to Understanding Web Application Development Corey Benson, SAS Institute, Inc., Cary, NC Robert Girardin, SAS Institute, Inc.

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

<option> eggs </option> <option> cheese </option> </select> </p> </form>

Java Server Pages (JSP)

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Recommended readings. Lecture 11 - Securing Web. Applications. Security. Declarative Security

Web-based IDE for Interfacing View Controller

Model-View-Controller. and. Struts 2

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

Specialized Programme on Web Application Development using Open Source Tools

Getting Started with Web Applications


HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Web Frameworks and WebWork

Get Success in Passing Your Certification Exam at first attempt!

How to use JavaMail to send

AD-HOC QUERY BUILDER

TCP/IP Networking, Part 2: Web-Based Control

An introduction to web programming with Java

In order for the form to process and send correctly the follow objects must be in the form tag.

Building Web Applications, Servlets, JSP and JDBC

Building Web Services with Apache Axis2

Sample HP OO Web Application

Adatbiztonság a gazdaságinformatikában

Specialized Programme on Web Application Development using Open Source Tools

Web Application Developer s Guide

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

JavaScript and Dreamweaver Examples

SPRING INTERVIEW QUESTIONS

Creating Java EE Applications and Servlets with IntelliJ IDEA

DESIGNING HTML HELPERS TO OPTIMIZE WEB APPLICATION DEVELOPMENT

Japan Communication India Skill Development Center

Japan Communication India Skill Development Center

PEAR. PHP Extension and Application Repository. Mocsnik Norbert Harmadik Magyarországi PHP Konferencia március 12., Budapest

Web Presentation Layer Architecture

2- Forms and JavaScript Course: Developing web- based applica<ons

Proposal for DSpace Web MVC

Multiple vulnerabilities in Apache Foundation Struts 2 framework. Csaba Barta and László Tóth

34. FELADATLAP KEZDŐKNEK KÉRDŐSZAVAK

Model-View-Controller: A Design Pattern for Software. June 2004

Lab 5 Introduction to Java Scripts

Research Article. ISSN (Print) *Corresponding author Lili Wang

Mini Project Report ONLINE SHOPPING SYSTEM

Web and Enterprise Applications Developer Track

Transcription:

Java technológiák 7. előadás A Struts keretrendszer Sapientia - EMTE 2010

Struts keretrendszer Bevezető Struts és az MVC tervezési minta Struts Action osztály fejlesztése Struts Action konfigurálása

Miért jó használni a Struts keretrendszert? Egyszerűsíti a webalkalmazások tervezését és implementációját. Egyszerűen lehet többnyelvű webalkalmazásokat készíteni. A Struts támogatja az MVC tervezési mintát, tehát megkönnyíti a komponensek elkülönítését. A Struts az MVC-ből, a vezérlés (Controller)réteget valósítja meg és a megjelenítési (View) réteget saját elemkönyvtárral támogatja. MVC

A Struts keretrendszer helye

MVC Java webalkalmazásokban M- modell vagy üzleti logika - POJO -Plain Old Java Object, JavaBeans V- megjelenítés (nézet) - főképp JSP, de lehet szervlet vagy bármi más is C- vezérlés- tipikusan egy szervlet, amely a kérés alapján eldönti, hogy mit kell elvégezni

A Front Controller tervezési minta (1)

A Front Controller tervezési minta (2)

A Front Controller tervezési minta (3) Feladatok Lokalizációt végezhet. Azonosítja az ügyfél típusát. Vezérli a felhasználó navigációját a nézetek között.

A Struts infrastruktúra

A Struts keretrendszer elemei A keretrendszer egy osztályhalmaz, amely egy adott infrastruktúrát támogat: Vezérlő szervlet: ActionServlet (CONTROLLER) Alaposztályok: Action, ActionForm (CONTROLLER) konfigurációs fájlok: struts-config.xml(controller) Struts elemkönyvtárak (VIEW)

Struts Action Servlet működésben Web szerver Web konténer Admin service execute AddCourse Action Student1 service service Action Servlet execute execute RegisterCourse Action Student2

Struts kérésfeldolgozás

Struts Action osztály ActionMapping findforward(name: String): ActionForward ActionForm ActionForward Action {abstract} execute( ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) :ActionForward <<interface>> HttpServletRequest <<interface>> HttpServletResponse RegisterAction execute(...):actionforward

Az execute metódus ActionMapping: akció tag ActionForm: űrlapbab az űrlap adatait tárolja HttpServletRequest: kérés HttpServletRequest: válasz ActionForward: melyik megjelenítést küldje vissza a felhasználónak

Példa AddDVD VIEW CONTROLLER AddDVDForm AddDVDAction add_dvd.jsp

ActionForm komponens public class AddDVDForm extends ActionForm { private String title; private String yearstr; private String genre; //get es set metodusok } public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) { //Validacio }

A validate metódus: title ActionErrors errors=new ActionErrors();... //validate title if(gettitle()==null gettitle().length()<1){ errors.add("title", new ActionMessage("error.title.required")); }... return errors;

A validate metódus: yearstr //URES if(getyearstr()==null getyearstr().length()<1){ errors.add("yearstr", new ActionMessage("error.yearStr.required")); } if( getyearstr()!= null ){ int year = -1; try{ year = Integer.parseInt(yearStr); }catch( NumberFormatException e ){ //NEM NUMERIKUS errors.add("yearstr", new ActionMessage( "error.yearstr.invalidyear")); } if( year!= -1 && (year<0 year>2050)){ //HELYTELEN EVSZAM errors.add("yearstr", new ActionMessage( "error.yearstr.invalidyear")); } }

Mit tartalmaz a struts-config.xml? <form-beans>...</form-beans> űrlap-babok <action-mappings>...</action-mappings> <action> <forward> <message-resources.../> erőforrásfájl

Egyszerű akcióleképzés <struts-config>... <action-mappings> <action path="/listazas" forward="list_courses.jsp"/>... </action-mappings>... </struts-config>

Bonyolult akcióleképzés <struts-config> <form-beans> <form-bean name="adddvdform" type="view.adddvdform"/> </form-beans> <action-mappings> <action path="/add_dvd" input="/add_dvd.jsp" name="adddvdform" scope="request" validate="true" type="controller.adddvdaction"> <forward name="success" path ="/success.jsp" /> <forward name="error" path ="/add_dvd.jsp" /> </action> </action-mappings>... </struts-config>

Hibák erőforrásfájl ApplicationResource #Add Course -- Ez egy megjegyzes sor error.name.required=hianyzo nev error.description.required=hianyzo leiras error.price.required=hianyzo ar error.price.nonnumeric=az ar nem szam error.price.negative=az ar nem pozitiv szam #misc. menu.menuitem1=listazas

Automatikus űrlapbab (Form-bean) ellenőrzés A bab validate metódusának hívását jelenti Az automatikus ellenőrzés a struts-config.xml állományban kérhető Ha nincs hiba, akkor hívódik az execute metódus Hiba esetében a validate metódus visszatéríti a hibalistát A hibalista a MessageResource segítségével nemzetköziesíthető, tehát e kérésnek megfelelő nyelven kerül vissza a bementi oldalra A kontroller szervlet a hibalistát lementi a kérés hatókörébe, visszairányít a bemeneti oldalra és megjeleníti a hibákat <html:errors/> tag segítségével.

Hibaüzenetek erőforrásfájl Fájlendszerbeli helye: \WEB-INF\classes\resources\ ApplicationResources.properties Konfiguráció: struts-config.xml <message-resources parameter= "resources.applicationresources"/> Tartalom: error.title.required= <li>please enter a title.</li> error.yearstr.required= <li>please enter the year.</li> error.yearstr.invalidyear= <li>year must be a positive int</li> error.genre.required= <li>please enter a genre.</li>

AddDVDAction komponens I public class AddDVDAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionErrors errors=new ActionErrors(); try{ AddDVDForm myform=(adddvdform)form; DvdItem item = new DvdItem( myform.gettitle(), myform.getyearstr(), myform.getgenre() );

AddDVDAction komponens II } request.setattribute("dvd", item); DVDLibraryDAO dao=new DVDLibraryDAO(); dao.insert(item); } catch(runtimeexception e){ errors.add(actionerrors.global_error, new ActionMessage( "error.unexpectederror", e.getmessage())); saveerrors(request, errors); } return mapping.findforward("success"); }

Struts program működése Ha a kérés egy olyan leképzésnek (mapping) felel meg, amely egy űrlapbabot (ActionForm) is tartalmaz, akkor: A központi vezérlőszervlet kinyeri vagy létrehozza az űrlapbabot A központi vezérlőszervlet továbbadja ezt az akcióobjektumnak (Action) Ha a kérés egy submit eredménye, az űrlapbab elvégezheti az adatok ellenőrzését Megjegyzések: Ha az adatok helyesek, továbbítódnak az üzleti rétegnek. Ha az adatok helytelenek, visszakerülnek a beviteli űrlapra a hibalistával együtt. Az ActionForm arra is használható, hogy az űrlapot feltöltsük kezdőértékekkel.

Struts elemkönyvtárak logic <logic:present name="person"> <bean:write name="person" property="firstname"/>... </logic:present> html <html:form.../> <html:text.../> <html:submit.../> bean <bean:define name="beanform" id="beanform" type="app.struts.forms.testsubmissionform"/>

Struts html elemkönyvtár form: HTML űrlap text: űrlap szövegdoboz radio:űrlap rádiógomb submit: űrlap Submit nyomógomb image: kép link: hiperlink errors: hibaüzenetek megjelenítése

Űrlap és űrlapbab(actionform) JSP szövegdoboz <input type="text" name="username" value="<%= loginbean.getusername()%>"/> Struts html elemkönyvtárral <html:text property="username"/>

Nemzetköziesítés ApplicationResources.properties course.name=name course.description=description course.price=price course.submit.step1=page 2 course.submit.step2=page 3 course.submit.finish=finish Használat JSP lap: <%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> <form action="add_course.do" method="post"> <bean:message key="course.name"/> <html:text property="name" size="50"/> <input type= submit value= <bean:message key="course.submit.step1"/> /> </form>