分享
 
 
 

[Struts]配置第一个Struts 应用

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有