Getting Started(Create new web site)
(For windows machine)
Install
- Install Apache Tomcat 6.0 and Eclipse IDE for Java EE Developers(Ganymede-SR1).
- Get Acornsnap from DownloadInstallers page, and install it(Acornsnap will overwrap Apache Tomcat installation).
- Install Eclipse plugin(sorry, it is manualy at the moment).
- The plugin(jar file) named "jp.co.lumber_mill.acorn3_tools_1.0.0.jar" had been placed at your desktop.
- Move the jar file into Eclipse's dropins folder.
- If installation is successful, you can see the icon of acorn in the toolbar.
- If the icon does not exist, try to relaunch by "eclipse.exe -clean".
- Launch eclipse with Java EE perspective, and do the following.
- Pursue along "Window > Preferences > Web", and set encoding to "ISO 10646/Unicode(UTF-8)" in CSS,HTML and JSP Files.
- Create new server(Tomcat 6.0) in Servers view(if the server already exists, delete it first).
- Create new Dynamic Web Project.
- Copy all files(and directories) from "$TOMCAT_HOME\webapps\acorn3-blank" to new project's "WebContent" folder(You will overwrite "WEB-INF" folder which already exists).
- Right-click on "WebContent/index.jsp", and pursue along "Run as > Run On Server" and click "Finish" button(without any changes).
- If you see the message "Welcome aboard", You accomplish!
And if the Eclipse pluing installed successfully, you see the icon below in Eclipse toolbar.
Make first action
In this section, we make a simple chat page(like Twitter) named Lwitter.
- Making your own application will proceed along MVC, rough scheme is given as follows:
- Create data definitions in "WebContent/WEB-INF/db".
- Generate models in "src/models".
- Generate controllers in "src/controllers".
- Generate views in "WebContent/WEB-INF/views".
- Run on server and testing it. If you add new objects in data definitions, you may need to "Clean.." server(on Servers view).
- First, stop Server(on Servers view) and right-click on "Tomcat v6.0" to execute "Clean..".
- Create new file named "WebContent/WEB-INF/db/dd.sql", and edit it as below:
CREATE TABLE lwitter ( id INTEGER NOT NULL IDENTITY, created_at TIMESTAMP NOT NULL DEFAULT now(), userid VARCHAR NOT NULL DEFAULT 'anonymous', mumble VARCHAR NOT NULL DEFAULT '', PRIMARY KEY(id) );
- Generate a model from lwitter table. Acornsnap's model(M) is a subclass of ActiveRecord class. Generated code is below:
package models; public class LwitterRecord extends jp.co.lumber_mill.database.ActiveRecord{ @jp.co.lumber_mill.database.PrimaryKey @jp.co.lumber_mill.database.NotNull public java.lang.Integer id; @jp.co.lumber_mill.database.NotNull public java.sql.Timestamp created_at; @jp.co.lumber_mill.database.NotNull public java.lang.String userid; @jp.co.lumber_mill.database.NotNull public java.lang.String mumble; @Override public String getTableName() { return "lwitter"; } @Override public String[] getNames() { return new String[]{"id","created_at","userid","mumble"}; } @Override public String[] getNamesAsLabel() { return new String[]{"ID","CREATED_AT","USERID","MUMBLE"}; } }package models; public class LwitterModel extends LwitterRecord { } - Generate controller named "Lwitter" and related views. Acornsnap's controller(C) is a subclass of AbstractController. And each action is defined as its public method. In this case, index method(=action) is only created. Acornsnap's view(V) is a plain JSP have same name as the method. In this case, the view is "index.jsp" in "WebContent?/WEB-INF/views/lwitter" folder.
- Edit "src/controllers/Lwitter.java" as below:
package controllers; import jp.co.lumber_mill.acornsnap.AbstractController; import jp.co.lumber_mill.database.Order; import models.LwitterModel; public class Lwitter extends AbstractController { @Override public void index() throws Exception { if("post".equalsIgnoreCase(request.getMethod())){ LwitterModel lm = new LwitterModel(); map(lm); save(lm); } LwitterModel lms[] = findAll(new LwitterModel(), 1, 20, null, Order.create("created_at DESC")); request.setAttribute("lms", lms); } } - Edit "WebContent/WEB-INF/views/lwitter/index.jsp" as below:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="models.LwitterModel"%> <%@page import="jp.co.lumber_mill.html.Form"%> <% LwitterModel[] lms = (LwitterModel[]) request.getAttribute("lms"); Form form = new Form("index",Form.POST); form.setModel(new LwitterModel()); %> <%= form.begin() %> <%= form.createTextInput("userid",6) %> <%= form.createTextInput("mumble",30) %> <%= form.createSubmitButton("Submit") %> <%= form.end() %> <hr/> <% if(lms == null || lms.length == 0){ out.println("<p>No data.</p>"); }else{ out.println("<table style='width: 100%'>"); for(int i=0;i<lms.length;i++){ %> <tr><td><%= lms[i].userid %></td><td><%= lms[i].mumble %></td></tr> <% } out.println("</table>"); } %> - Finally, start the server(on Servers view), and open the url http://localhost:8080/(Name of your project)/action/lwitter/index.
Followings are all you have created.
- (your project)
- Java Resources: src/
- controllers/
- Lwitter.java
- models/
- LwitterModel.java
- LwitterRecord.java
- controllers/
- WebContent/
- WEB-INF/
- db/
- dd.sql
- views/
- lwitter/
- index.jsp
- lwitter/
- db/
- WEB-INF/
- Java Resources: src/
Deploy to server machine
(For linux machine)
In this section, We will deploy and run webapp into a server machine running with Fedora.
First, you need to install Apache Tomcat 6.0 using 'yum' command, and give a user the authority to execute any command as root and tomcat user via 'visudo' command.
# service sshd start # yum -y install tomcat6 # visudo
(visudo's example:)
#Defaults requiretty -- Comment out or remove this line. username ALL =(root,tomcat) NOPASSWD: ALL
Then you may need to create database directory if you want to store the files of databases.
# mkdir /var/lib/tomcat6/databases # chown root:tomcat /var/lib/tomcat6/databases
Once you've got them, back to Eclipse(with Acornsnap plugin).
- Select the web project, and click Acornsnap's toolbar icon(Acornsnap tools will launch).
- Pursue along "Action > Deploy to remote host..".
- Add new host formatted as "username@hostname:sshport", and keep selecting the row.
- Check "Send acornsnap files". You need it only first.
- Click "Deploy".
Attachments
- welcomeaboard.png (18.7 KB) - added by yosei 17 months ago.
- lwitter.png (14.9 KB) - added by yosei 17 months ago.
- tools-deployer.png (13.5 KB) - added by yosei 16 months ago.
- tools-icon.png (1.0 KB) - added by yosei 16 months ago.



