http://community.csdn.net/expert/topic/3517/3517995.xml?temp=.2898371
这是我的第一个struts应用,仅仅用于用户注册;注册的用户存入数据库中。
参考《jsp应用开发详解》电子工业出版社
part i/iii
/**
sql server 2000
tomcat 4.1
struts jakarta-struts-1.1
editplus
*/
//----数据库脚本----
create database dba
create table tuser(uname varchar(64),upassword varchar(64),uage int)
//----tomcat 配置----
<context path="/struts" docbase="e:/struts" debug="0"
reloadable="true" crosscontext="true">
</context>
//----classpath 设置----
//由于我用的是基本文本编辑器,所以struts的jar 文件需要手工配置到classpath中
.;e:/struts/web-inf/classes;e:/struts/web-inf/lib;d:/webset/jdk/lib/dt.jar;d:/webset/jdk/lib/tools.jar;d:/webset/jdk/lib/msbase.jar;d:/webset/jdk/lib/mssqlserver.jar;d:/webset/jdk/lib/msutil.jar;d:/webset/jdk/lib/itextasian.jar;d:/webset/jdk/lib/jxl.jar;e:/struts/web-inf/lib/struts.jar;e:/struts/web-inf/lib/commons-beanutils.jar;e:/struts/web-inf/lib/commons-collections.jar;e:/struts/web-inf/lib/commons-digester.jar;e:/struts/web-inf/lib/commons-fileupload.jar;e:/struts/web-inf/lib/commons-lang.jar;e:/struts/web-inf/lib/commons-logging.jar;e:/struts/web-inf/lib/commons-validator.jar
//----struts 配置-----
e:/struts/web-inf/lib 的目录
2003-06-29 118,726 commons-beanutils.jar
2003-06-29 165,119 commons-collections.jar
2003-06-29 109,096 commons-digester.jar
2003-06-29 22,379 commons-fileupload.jar
2003-06-29 63,980 commons-lang.jar
2003-06-29 31,605 commons-logging.jar
2003-06-29 46,865 commons-validator.jar
2003-06-29 498,051 struts.jar
【2002-05-03 302,282 msbase.jar】
【2002-05-03 69,477 mssqlserver.jar】
【2002-05-03 67,235 msutil.jar】
e:/struts/web-inf/tld 的目录
2003-06-29 8,868 struts-bean.tld
2003-06-29 66,192 struts-html.tld
2003-06-29 14,511 struts-logic.tld
2003-06-29 64,659 struts-nested.tld
2003-06-29 1,631 struts-template.tld
2003-06-29 7,850 struts-tiles.tld
//---- web-inf/web.xml ----
<?xml version="1.0" encoding="iso-8859-1"?>
<!doctype web-app
public "-//sun microsystems, inc.//dtd web application 2.3//en"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>struts blank application</display-name>
<!--standard action servlet configuration(with debugging)-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>applicationresources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!--standard action servlet mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--the usual weblcome file list-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--struts tag library descriptions-->
<taglib>
<taglib-uri>/struts-bean</taglib-uri>
<taglib-location>/web-inf/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-html</taglib-uri>
<taglib-location>/web-inf/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-logic</taglib-uri>
<taglib-location>/web-inf/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-nested</taglib-uri>
<taglib-location>/web-inf/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-tiles</taglib-uri>
<taglib-location>/web-inf/tld/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-template</taglib-uri>
<taglib-location>/web-inf/tld/struts-template.tld</taglib-location>
</taglib>
</web-app>
//---- web-inf/struts-config.xml ----
<?xml version="1.0" encoding="iso-8859-1"?>
<!doctype struts-config public
"-//apache software foundation//dtd struts configuration 1.1//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name = "userform" type="com.stru.userform" />
</form-beans>
<global-forwards>
<forward name = "usercreated" path="/viewuser.jsp" />
</global-forwards>
<action-mappings>
<action path = "/createuser"
type = "com.stru.useraction"
name = "userform"
scope= "request"
validate="true"
input ="/createuser.jsp">
</action>
</action-mappings>
<message-resources parameter = "applicationresources" />
</struts-config>
//---- web-inf/applicationresources.properties ----
index.title = struts tutorial
part ii/iii
//---- 类的编写 ----
e:/struts/web-inf/classes/com/stru 的目录
dbcon.class //封装基本的数据库操作
user.class //基本的 getter,setter 方法
userbean.class //实现具体的业务逻辑(通常是数据库的操作),被action 类调用
useraction.class //action
userform.class //form
//---- dbcon.java ----
package com.stru;
/**
基本的数据库操作类
*/
import java.sql.*;
import java.net.*;
import java.util.stringtokenizer;
public class dbcon{
string driverstr="com.microsoft.jdbc.sqlserver.sqlserverdriver";
string dbip="127.0.0.1";
string dbname="dba";
string dbuser="sa";
string dbpwd="jnxr@))$";
string errtag="";
string dburl="";
public void setdbip(string value){
dbip=value;
}
public string getdbip(){
return dbip;
}
public void setdbname(string value){
dbname=value;
}
public string getdbname(){
return dbname;
}
public void setdbuser(string value){
dbuser=value;
}
public string getdbuser(){
return dbuser;
}
public void setdbpwd(string value){
dbpwd=value;
}
public string getdbpwd(){
return dbpwd;
}
public void seterrbz(string value){
errtag=value;
}
public string geterrbz(){
return errtag;
}
public boolean dbtry(){
try{
dburl="jdbc:microsoft:sqlserver://"+dbip+":1433;databasename="+dbname;
class.forname(driverstr); //加载驱动
connection conn= drivermanager.getconnection(dburl,dbuser,dbpwd); //加载数据库连接
return true;
}catch(exception e){
return false;
}
}
public string dbtest(){
try{
dburl="jdbc:microsoft:sqlserver://"+dbip+":1433;databasename="+dbname;
class.forname(driverstr); //加载驱动
connection conn= drivermanager.getconnection(dburl,dbuser,dbpwd); //加载数据库连接
return "database connection ok!";
}catch(exception e){
return "error:/n"+e.tostring();
}
}
public void dbexe(string sqlstr){
connection conn=null;
statement stmt=null;
try{
dburl="jdbc:microsoft:sqlserver://"+dbip+":1433;databasename="+dbname;
class.forname(driverstr);
conn= drivermanager.getconnection(dburl,dbuser,dbpwd);
stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
stmt.execute(sqlstr);
errtag="ok";
}catch(exception e){
system.out.println(e.tostring());
system.out.println(sqlstr);
errtag="error";
}finally{
try{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(sqlexception ee){
system.out.println(ee.tostring());
}
}
}
public resultset dbqry(string sqlstr){ //数据源未关闭
connection conn=null;
statement stmt=null;
resultset rs=null;
try{
dburl="jdbc:microsoft:sqlserver://"+dbip+":1433;databasename="+dbname;
class.forname(driverstr);
conn= drivermanager.getconnection(dburl,dbuser,dbpwd);
stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
rs=stmt.executequery(sqlstr);
errtag="ok";
}catch(exception e){
system.out.println(e.tostring());
system.out.println(sqlstr);
errtag="error";
}
return rs;
}
public int qnum(string sqlstr){
connection conn=null;
statement stmt=null;
resultset rs=null;
int num=-1;
try{
dburl="jdbc:microsoft:sqlserver://"+dbip+":1433;databasename="+dbname;
class.forname(driverstr);
conn= drivermanager.getconnection(dburl,dbuser,dbpwd);
stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
rs=stmt.executequery(sqlstr);
if(rs.next()){
num=rs.getint(1);
errtag="ok";
}
}catch(exception e){
system.out.println(e.tostring());
system.out.println(sqlstr);
errtag="error";
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(sqlexception ee){
system.out.println(ee.tostring());
}
}
return num;
}
}
//---- user.java ----
package com.stru;
/**
最基本的getter,setter 方法
*/
public class user {
public void setname(string aname){
this.name = aname;
}
public string getname(){
return this.name;
}
public void setpassword(string apassword){
this.password = apassword;
}
public string getpassword(){
return this.password ;
}
public void setage(int aage){
this.age = aage;
}
public int getage(){
return this.age ;
}
public static void main(string[] args) {
system.out.println("current user");
}
private string name;
private string password;
private int age;
}
part iii/iii
//---- userbean.java -----
package com.stru;
import java.sql.*;
import javax.sql.*;
import java.io.*;
/**
实现具体的业务逻辑(通常是数据库的操作),被action 类调用;
但和httprequest 无关
*/
public class userbean {
private dbcon dbcon ;
public userbean () throws exception{
dbcon = new dbcon();
}
public void adduser(user user) throws sqlexception{
string sql = "insert into tuser(uname,upassword,uage) values('"+user.getname()+"','"+user.getpassword()+"',"+user.getage()+")";
dbcon.dbexe(sql);
}
}
//---- useraction.java ----
package com.stru;
import javax.servlet.http.*;
import org.apache.struts.action.* ;
/**
action
*/
public class useraction extends action {
public actionforward perform(actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse response){
userform f =(userform)form;
try{
userbean userbean = new userbean();
userbean.adduser(f.getuser());
}
catch (exception e){
e.printstacktrace();
}
request.setattribute("user",f.getuser());//
return (mapping.findforward("usercreated"));
}
}
//---- userform.java ----
package com.stru;
import javax.servlet.http.httpservletrequest;
import org.apache.struts.action.* ;
/**
form
*/
public class userform extends actionform {
public void setusername(string ausername){
user.setname(ausername);
}
public string getusername(){
return user.getname();
}
public void setuser(user user){
this.user = user ;
}
public user getuser(){
return this.user ;
}
/**
reset the form
*/
public void reset(actionmapping mapping,httpservletrequest request){
this.user = new user() ;
}
/**
check the form
*/
public actionerrors validate(actionmapping mapping,httpservletrequest request){
actionerrors errors = new actionerrors() ;
if (user.getname() == null || user.getname().trim().equals("") || user.getpassword().trim().equals("")){
errors.add("userinputerror",new actionerror("errorinput:name/password"));
}
return errors ;
}
private user user = new user();
}
//---- jsp 页面的编写 ----
2004-11-15 createuser.jsp //输入区域
2004-11-15 viewuser.jsp //执行返回
//---- createuser.jsp -----
<%@ page contenttype="text/html; charset=gb2312"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<html:html locale ="true">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<html:base/>
<title><bean:message key = "index.title" /></title>
</head>
<body>
<h2>创建一个新用户</h2>
<html:errors/>
<html:form action = "createuser.do" method="get">
name:<html:text property = "user.name" /></br>
password:<html:password property = "user.password" /></br>
age :<html:text property = "user.age" /></br>
<html:submit property ="submit" />
</html:form>
</body>
</html:html>
//----- viewuser.jsp ----
<%@ page contenttype="text/html; charset=gb2312" import="com.stru.user"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<html:html locale ="true">
<head>
<html:base/>
<title><bean:message key = "index.title" /></title>
</head>
<body>
<%user user = (user)request.getattribute("user"); %>
已经创建了用户7<br>
name:<%=user.getname()%><br>
password:<%=user.getpassword()%><br>
age:<%=user.getage()%><br>
</body>
</html:html>
//---- 最后测试 ----
http://localhost:8080/struts/createuser.jsp
闽公网安备 35060202000074号