45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:配置Struts应用的方法

配置Struts应用的方法

2016-08-30 12:51:41 来源:www.45fan.com 【

配置Struts应用的方法

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-29118,726 commons-beanutils.jar
2003-06-29165,119 commons-collections.jar
2003-06-29109,096 commons-digester.jar
2003-06-2922,379 commons-fileupload.jar
2003-06-2963,980 commons-lang.jar
2003-06-2931,605 commons-logging.jar
2003-06-2946,865 commons-validator.jar
2003-06-29498,051 struts.jar
【2002-05-03302,282 msbase.jar】
【2002-05-0369,477 mssqlserver.jar】
【2002-05-0367,235 msutil.jar】

E:/struts/WEB-INF/tld 的目录
2003-06-298,868 struts-bean.tld
2003-06-2966,192 struts-html.tld
2003-06-2914,511 struts-logic.tld
2003-06-2964,659 struts-nested.tld
2003-06-291,631 struts-template.tld
2003-06-297,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-15createUser.jsp//输入区域
2004-11-15viewUser.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

 

 

 

本文地址:http://www.45fan.com/dnjc/69801.html
Tags: 配置 HTTP Struts
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部