+ All Categories
Home > Documents > MVC/Struts 1 - University of California, San...

MVC/Struts 1 - University of California, San...

Date post: 11-May-2018
Category:
Upload: letuyen
View: 221 times
Download: 0 times
Share this document with a friend
20
MVC/Struts 1 How to set up everything
Transcript

MVC/Struts 1

How to set up everything

Servlet ServletServlet /

Static pages

Servlet & Struts

Servlet Container

servlet(java methodwritten by us)

HTT

P R

eque

st

A step by step tutorial

Functionality: input and display staff info (first name, last name, position)Workflow

If the staff info is new, return to home pageIf the staff info already exists in the db, error 1If the staff info conflicts with data in db, error 2

1. Set up Tomcat – install

Download Tomcat 5.5 or 6.0 and install ithttp://localhost:8080

Note: for Linux, default port number is 8180

1. Set up Tomcat – env varaibles

Set the environment vairableClasspath: servlet-api.jar, jsp-api.jarCATALINA_HOME: D:\Program Files\Apache Software Foundation\Tomcat 5.5CATALINA_BASE: D:\Program Files\Apache Software Foundation\Tomcat 5.5

1. Set up tomcat – test Servlet

Download multiplier package from http://db.ucsd.edu/CSE135S09/demos/multiplier.zipUnzip it under \webapp\

\multiplier should be under \webapp\directly

Visit http://localhost:8080/multiplier/multiplier.html

1. Set up Tomcat – errors

If the demo doesn’t work …Check the newest log files under D:\Program Files\Apache Software Foundation\Tomcat 5.5\logsTomcat dumps all the errors into log. It is easier for you to check what is the error.

For Linux:It is very likely the demo fails, because of the default security policies of Tomcat. Come to me for more help.

2. Set up DB – install

Download MySQL (PostgreSQL, etc) and install it.By default, the DB should start automatically. To double check, check the background processes

MySQL: mysqldPostgreSQL: postgres

2. Set up DB – client terminal

All the DBs provide client terminal

2. Set up DB – create DB

Create database and tables

2. Set up DB – try JDBC

Download JDBC packageIt’s a jar package: mysql-connector-java-5.1.7-bin.jarAdd it to the classpath

A simple java program showing the tuples we just inserted

http://db.ucsd.edu/CSE135S09/demos/Test.java

2. Set up DB – JDBCimport java.sql.*;public class Test {

public static void main(String args[]) throws Exception {Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=root");if (conn == null) {

System.out.println("open fails."); System.exit(-1);}

Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from person");while (rs.next()) {

int id = rs.getInt("pid");String firstname = rs.getString("firstname");String lastname = rs.getString("lastname");

System.out.println("id: " + id + ", first name: " + firstname+ "last name: " + lastname);

}rs.close();stmt.close();conn.close();

}}

2. Set up DB – JDBC

3. Set up the demoDownload demo from http://db.ucsd.edu/CSE135S09/demos/staff.zip

Unzip \staff directly under \webappVisit http://localhost:8080/staff

error1

Input

3. Set up the demo

What you may need to modify in your machine

staff\WEB-INF\src\java\DB.javaIf you use different DB server and thus different JDBC, remember to put JDBC driver under \staff\WEB-INF\lib

Misc: Download Struts

Download struts 1.3.xStruts is only a package. It contains:

\doc, \src, \lib, \appsWithin \apps is a set of *.war files

struts-blank-1.3.9.warstruts-examples-1.3.9.war

Set the classpathStruts-core-1.3.9.jar

Start your own 1

Put struts-blank-1.3.x.war under \webapps and restart tomcat. Automatically deploy a new app dir

Rename \struts-blank-1.3.x\ to \your_app_name\However, tld files may not be contained in this package, you need to add them manually

Start your own 2Add the following to web.xml and download corresponding tld files in staff\WEB-INF\

<taglib><taglib-uri>/tags/struts-bean</taglib-uri><taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib><taglib>

<taglib-uri>/tags/struts-html</taglib-uri><taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib><taglib>

<taglib-uri>/tags/struts-logic</taglib-uri><taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

Start your own 2

Or just modify the demo“staff” package already contains all the tld filesRewrite all the java src (form beans, actions)rewrite the struts-config.xml to your own flow


Recommended