File Upload And Download Example In Struts 2 Tutorial
Struts 2 Hello World Example. By mkyong June 8, 2. Updated August 2. Viewed 3. 71,0. In this example, we show you how to create a hello world example in Struts 2. The following libraries or tools are used Maven 3. Introduction of Struts 2 Framework. Apache Struts 2 is an elegant, extensible framework for creating enterpriseready Java web applications. The framework is designed. Java programming tutorials, articles, tips, code examples and demo programs of various categories Java core, Java SE, Java EE, Spring framework, Struts framework. HibernatePic5.gif' alt='File Upload And Download Example In Struts 2 Tutorial' title='File Upload And Download Example In Struts 2 Tutorial' />Spring Hibernate Integration Example Tutorial for Hibernate 3 Hibernate 4. Spring ORM example, Hibernate Configuration, Annotations project in Eclipse. Tutorials Point, Simply Easy Learning 1 P a g e Apache Struts 2 Tutorial Tutorialspoint. Struts2 is popular and mature web application framework based on the MVC. File Upload And Download Example In Struts 2 Tutorial' title='File Upload And Download Example In Struts 2 Tutorial' />Hello World Example with Struts in Eclipse IDE. Let us go through a small hello world example with struts in Eclipse and take practical overview of code level. Welcome to the last part of 7 article series of Struts 2 Framework tutorials. In previous article we saw how to implement File Upload functionality in Struts 2. In. Spring File Upload,Spring MVC File Upload example MultipartFile, MultipartResolver, maxUploadSize, Apache Commons FileUpload, single and multiple files. Eclipse 3. 7. Struts 2. Final project structure. Let review the final project structure of this tutorial, in case you get lost in later steps. Struts. 2 dependencies. Use Maven to download the entire Struts. Add struts. 2 core in pom. File pom. xml. lt project xmlnshttp maven. POM4. 0. 0. xmlns xsihttp www. XMLSchema instance. Locationhttp maven. POM4. 0. 0. http maven. Version 4. 0. Version. Id com. mkyong. Id. Id Struts. 2Examplelt artifact. Id. lt packaging warlt packaging. Struts. 2Example Maven Webapplt name. Id junitlt group. Id. lt artifact. Id junitlt artifact. Id. lt version 3. Id org. apache. Id. Id struts. 2 corelt artifact. Id. lt version 2. Name Struts. 2Examplelt final. Name. lt plugins. Id maven compiler pluginlt artifact. Id. lt version 2. Convert to Eclipse project. Compile and convert the to Eclipse web project in command prompt. Dwtpversion2. 0. Review the Eclipse. Struts. 2 dependencies are downloaded File. M2REPOasmasm3. M2REPOasmasm commons3. M2REPOasmasm tree3. M2REPOcommons fileuploadcommons fileupload1. M2REPOcommons iocommons io2. M2REPOcommons langcommons lang2. M2REPOorgfreemarkerfreemarker2. M2REPOjavassistjavassist3. GAjavassist 3. 1. GA. jar. lt classpathentry kindvar pathM2REPOjunitjunit3. M2REPOognlognl3. M2REPOorgapachestrutsstruts. C Program FilesJavajdk. M2REPOorgapachestrutsxworkxwork core2. JRECONTAINER. JSP view pages. A JSP login page to use the Struts 2 tags to display username and password input fields and submit button. Fie login. jsp. Typetexthtml charsetUTF 8. Struts 2 Hello World Examplelt h. Welcome. lt s textfield nameusername labelUsername. Password. lt s submit. File welcomeuser. A JSP view page to display a welcome message to user. Typetexthtml charsetUTF 8. Struts 2 Hello World Examplelt h. Both Struts 1 and Struts 2 has very similar UI tags syntax, just a little different in term of naming the HTML elements, for example Struts 1. Welcome. lt html text propertyusername. Welcome. lt s textfield nameusername labelUsername. Action, put all business logic here. A simple Struts. 2 Action class, its used to declared all the business logic inside. File Welcome. User. Action. java. package com. Welcome. User. Action. String username. public String get. Username. return username. UsernameString username. String execute. SUCCESS. In Struts. Action class is not required to implement any interface or extend any class, but its required to create an execute method to put all the business logic inside and return a String value to tell user where to redirect. Note. You may see some users implement the com. Action class, but its totally optional, because the com. Action is just provide some handy constant values only. Note. Struts. 1s Action class is required to extends the org. Action. But Struts 2 Action class is optional, but you are still allow to implement the com. Action for some handy constant values or extends the com. Action. Support for some common default Action implementation functions. Struts configuration file. A Strut configuration file to link all stuff together. The xml file name must be struts. File struts. xml. UTF 8. lt DOCTYPE struts PUBLIC. Apache Software FoundationDTD Struts Configuration 2. EN. http struts. User extendsstruts default. Login. lt result pageslogin. Welcome classcom. Welcome. User. Action. SUCCESS pageswelcomeuser. Declare a package and warp the action classes inside, the action classes are self explanatory, but you may interest at following new tag 1. Just a package name, dont really care about it. UserIts used to match the User URL pattern. See this article Struts 2 namespace example and explanation. It means the package is extends the struts default package components and interceptors, which is declared in the struts default. Configure the Web Application Deployment Descriptor web. Struts. 2 to your web project. File web. xml. lt DOCTYPE web app PUBLIC. Sun Microsystems, Inc. DTD Web Application 2. EN. http java. Struts 2 Web Applicationlt display name. Struts. Prepare. And. Execute. Filter. lt filter class. Run it. In Struts. Struts. 2ExampleUserLogin. Struts. 2ExampleUserWelcome. Download It. Loading. Spring Hibernate Integration Example Tutorial. Spring is one of the most used Java EE Framework and Hibernate is the most popular ORM framework. Thats why Spring Hibernate combination is used a lot in enterprise applications. Recently I have written a lot for Spring Tutorial and Hibernate Tutorial, so a post for spring hibernate integration was due for long time. Today in this tutorial, we will use Spring 4 and integrate it with Hibernate 3 and then update the same project to use Hibernate 4. Since there are a lot of versions for Spring and Hibernate both and Spring ORM artifact supports both Hibernate 3 and Hibernate 4, its good that I list all the dependencies I have used in my project. Note that I have noticed that all spring and hibernate versions are not compatible, below versions have worked for me so I think they are compatible. If you are using some other versions and getting java. No. Class. Def. Found. Error, then it means that they are not compatible. Mostly its because Hibernate classes are moved from one package to another causing this error. For example org. hibernate. Filter. Definition class is moved to org. Filter. Definition in latest hibernate versions. Spring Framework Version 4. RELEASEHibernate Core and Hibernate Entity. Manager Version 3. Final and 4. 3. 5. Final. Spring ORM Version 4. RELEASEDatabase Setup. I am using My. SQL database for my project, so below setup. CREATE TABLE Person. NOT NULL AUTOINCREMENT. NOT NULL DEFAULT. DEFAULT NULL. PRIMARY KEY id. ENGINEInno. DB AUTOINCREMENT5 DEFAULT CHARSETutf. Spring Hibernate Integration Example Project Structure. Below image shows the final project structure, we will go through each of the components one by one. Maven Dependencies. We will first look into our pom. POM4. 0. 0 xmlns xsihttp www. XMLSchema instance. Locationhttp maven. POM4. 0. 0 http maven. Version 4. 0. Version. Id org. springframework. Id. lt artifact. Id Spring. Hibernate. Examplelt artifact. Id. lt version 0. SNAPSHOTlt version. Generic properties. Encoding UTF 8lt project. Encoding. lt project. Encoding UTF 8lt project. Encoding. lt Spring. RELEASElt spring framework. Hibernate JPA. Finallt hibernate. Finallt hibernate. Logging. lt logback. Spring and Transactions. Id org. springframeworklt group. Id. lt artifact. Id spring contextlt artifact. Id. lt version spring framework. Id org. springframeworklt group. Id. lt artifact. Id spring txlt artifact. Id. lt version spring framework. Spring ORM support. Id org. springframeworklt group. Id. lt artifact. Id spring ormlt artifact. Id. lt version spring framework. Logging with SLF4. J Log. Back. Id org. Id. lt artifact. Id slf. Id. lt version slf. Id ch. qos. logbacklt group. Id. lt artifact. Id logback classiclt artifact. Id. lt version logback. Hibernate. Id org. Id. lt artifact. Id hibernate entitymanagerlt artifact. Id. lt version hibernate. Id org. hibernatelt group. Id. lt artifact. Id hibernate corelt artifact. Id. lt version hibernate. Id mysqllt group. Id. lt artifact. Id mysql connector javalt artifact. Id. lt version 5. Id commons dbcplt group. Id. lt artifact. Id commons dbcplt artifact. Id. lt version 1. Important Dependencies for Spring and Hibernate Integration Project are spring context and spring tx for core Spring functionalities. Notice that I am using version 4. RELEASE. spring orm dependency for Spring ORM support, its required for hibernate integration in our spring project. Hibernate framework. Notice that version is 3. Final, for using Hibernate 4 all we need is to change it to 4. Final as commented in above pom. My. SQL driver for database connection. Model Class or Entity Bean. We can use Hibernate XML based mapping as well as JPA annotation based mapping. Here I am using JPA annotations for mapping because hibernate provides JPA implementation. Column. import javax. Entity. import javax. Generated. Value. Generation. Type. Id. import javax. Table. Entity bean with JPA annotations. Hibernate provides JPA implementation. TablenamePerson. Person. Columnnameid. Generated. ValuestrategyGeneration. Type. IDENTITY. private int id. String name. private String country. Id. public void set. Idint id. this. String get. Name. NameString name. String get. Country. CountryString country. String to. String. DAO Classes. We will implement two methods in our DAO classes, first to save the Person object into table and second that will fetch all the records from the table and returns the list of Persons. List. import com. Person. public interface Person. DAO. public void savePerson p. Listlt Person list. Above DAO class implementation would be like below. List. import org. Session. import org. Session. Factory. Transaction. import com. Person. public class Person. DAOImpl implements Person. DAO. private Session. Factory session. Factory. Session. FactorySession. Factory session. Factory. Factory session. Factory. Person p. Session session this. Factory. open. Session. Transaction tx session. Transaction. session. Suppress. Warningsunchecked. Listlt Person list. Session session this. Factory. open. Session. Listlt Person person. List session. create. Queryfrom Person. List. Notice that this is the only place where we are using Hibernate related classes. This pattern makes our implementation flexible and easy to migrate from one technology to another. For example, if we want to use i. Batis ORM framework, all we need is to provide a DAO implementation for i. Batis and then change the spring bean configuration file. In above example, I am using Hibernate session transaction management. But we can also use Spring declarative transaction management using Transactional annotation, read more at Spring Transaction Management. Spring Bean Configuration File for Hibernate 3 Integration. Lets first look at the spring bean configurations we need for Hibernate 3 integration, we will look into detail later on. UTF 8. lt beans xmlnshttp www. XMLSchema instance xmlns aophttp www. Locationhttp www. Source classorg. Basic. Data. Source. Class. Name valuecom. Driver. Test. DB. lt property nameusername valuepankaj. Hibernate 3 XML Session. Factory Bean definition. Session. Factory. Local. Session. Factory. Bean. lt property namedata. Source refdata. Source. Resources. lt list. Properties. lt value. My. SQLDialect. lt value. Hibernate 3 Annotation Session. Factory Bean definition. Annotated. Session. Factory. classorg. Annotation. Session. Factory. Bean. Source refdata. Source. lt property nameannotated. Classes. lt list. Personlt value. Properties. My. SQLDialectlt prop. DAO classcom. journaldev. Person. DAOImpl. Factory refhibernate. Internet Manager 5.19 Serial Number more. Annotated. Session.