struts初学者的苦恼 能不能详细点。最好把你的代码copy出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 web.xml:<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app> <!-- Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>com.evertech.test.ApplicationResources</param-value><!--ApplicationResources.properties已经建立好在WEB-INF/src/com/evertech/test目录下--> </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>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>validate</param-name> <param-value>true</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib></web-app><!--=====================End=========================-->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="userinfoForm" type="com.evertech.test.UserinfoForm"/> </form-beans> <action-mappings> <action path="/adduser" type="com.evertech.test.AddUserAction" attribute="userinfoForm" scope="request" input="/userinfo.jsp" validate="true"> <forward name="success" path="/userinfo.jsp"/> </action> </action-mappings> <message-resources parameter="com.evertech.test.ApplicationResources"/></struts-config>ApplicationResources.properties:userinfo.title=userinfo listerror.username.required=<li>Username is required</li>error.password.required=<li>Password is required</li>error.db.add = database error!!!!!!!!!!########=============END==========userinfo.jsp<%@ page language = "java" %><%@ taglib uri = "WEB-INF/struts-bean.tld" prefix = "bean" %><%@ taglib uri = "WEB-INF/struts-html.tld" prefix = "html" %><%@ taglib uri = "WEB-INF/struts-logic.tld" prefix = "logic" %><html:html locale = "true"><head><html:base/><title><bean:message key="userinfo.title"/></title></head><body background = "white"><h3>user information</h3><table width="400" border="0" align="center"><html:form action = "adduser.do" focus ="username" method = "GET"> <html:text property = "username" maxlength = "16"/> <html:text property = "password" maxlength = "10"/> <html:submit property = "add"/></html:form></table></body></html:html>下面还有 UserinfoForm.javapackage com.evertech.test;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.*;public final class UserinfoForm extends ActionForm{ private String username; private String password; public void setusername(String username) { this.username = username; } public String getusername() { return this.username; } public void setpassword(String password) { this.password = password; } public String getpassword() { return this.password; } public void reset(ActionMapping actionmapping, HttpServletRequest httpservletrequest) { this.username = null; this.password = null; } public ActionErrors validate(ActionMapping actionmapping, HttpServletRequest httpservletrequest) { ActionErrors actionerrors = new ActionErrors(); if(username == null || username.length() < 1) actionerrors.add("username", new ActionError("error.username.required")); if(password == null || password.length() < 1) actionerrors.add("password", new ActionError("error.password.required")); return actionerrors; }}AddUserAction.java:package com.evertech.test;import java.sql.*;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.*;import org.apache.struts.config.ActionConfig;public class AddUserAction extends Action{ public static EverLog log = EverLog.getInstance(com.evertech.test.AddUserAction.class); public ActionForward execute(ActionMapping actionmapping, ActionForm actionform, HttpServletRequest request, HttpServletResponse response) { org.apache.struts.util.MessageResources messageresources = getResources(request); ActionErrors errors = new ActionErrors(); Connection connection; PreparedStatement preparedstatement; String username = request.getParameter("username"); String password = request.getParameter("password"); try { Database database = new Database(); //自定义的一个连接数据库的类 connection = database.getConn(); //取得一个连结 String sql = "insert into userinfo( username,password ) values(?,?)"; preparedstatement = connection.prepareStatement(sql); preparedstatement.setString(1, username); preparedstatement.setString(2, password); preparedstatement.executeUpdate(); } catch(SQLException sqlexception) { log.debug("SQL execute Error:" + sqlexception); errors.add("databaseerror", new ActionError("error.db.add")); connection = null; preparedstatement = null; } finally { connection = null; preparedstatement = null; } return actionmapping.findForward("success"); }}Database.java:package com.evertech.test;import java.sql.Connection;import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;public class Database{ public static EverLog log = EverLog.getInstance(com.evertech.test.Database.class); public Connection getConn() { Connection connection; try { InitialContext initialcontext = new InitialContext(); if(initialcontext == null) throw new Exception("No Context"); Context context = (Context)initialcontext.lookup("java:/comp/env"); DataSource datasource = (DataSource)context.lookup("jdbc/myoracle"); connection = datasource.getConnection(); } catch(Exception exception) { log.debug("database connetion exception:" + exception); connection = null; } return connection; }}createtable.sql:create table userinfo( username varchar2(20) primary key, password varchar2(10) not null )全都贴出来了,盼解决,谢谢 取数据不时这样取的request.getParameter时这样的 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { NewForm newForm = (NewForm)form;....... <action path="/adduser" type="com.evertech.test.AddUserAction" attribute="userinfoForm" scope="request" input="/userinfo.jsp" validate="true"> <forward name="success" path="/userinfo.jsp"/> </action>中attribute="userinfoForm"应该是name="userinfoForm"name才是用来指定form-bean的 忘了说,attribute属性在name属性没有指定的情况下是不被允许的这个你可以看看strust-config.dtd里面的注释写得很明白还有request.getParameter这种方式也可以取到数据,并没有影响 java的applet小程序 java word的处理 JAVA中 引用的内存(栈中的)大小是多少 如何判断 Swt与Jface 的连环 Dialog 状态? 传值问题 各位前辈高手!请指点! 我想学java,但是有很多问题,困惑,,,,,,,,,, 大家帮个忙,请问登录页面的用户名和密码如何同数据库的密码相判断?? 一道关于内部类的SCJP题?请帮忙解释一下 根据URL下载网页源代码 jbuilder7的问题 关于JTable标题的问题
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app> <!-- Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>com.evertech.test.ApplicationResources</param-value>
<!--ApplicationResources.properties已经建立好在WEB-INF/src/com/evertech/test目录下-->
</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>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib></web-app>
<!--=====================End=========================-->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="userinfoForm"
type="com.evertech.test.UserinfoForm"/>
</form-beans> <action-mappings>
<action path="/adduser"
type="com.evertech.test.AddUserAction"
attribute="userinfoForm"
scope="request"
input="/userinfo.jsp"
validate="true"> <forward name="success" path="/userinfo.jsp"/>
</action>
</action-mappings>
<message-resources
parameter="com.evertech.test.ApplicationResources"/>
</struts-config>ApplicationResources.properties:userinfo.title=userinfo listerror.username.required=<li>Username is required</li>
error.password.required=<li>Password is required</li>error.db.add = database error!!!!!!!!!!########=============END==========userinfo.jsp<%@ page language = "java" %><%@ taglib uri = "WEB-INF/struts-bean.tld" prefix = "bean" %>
<%@ taglib uri = "WEB-INF/struts-html.tld" prefix = "html" %>
<%@ taglib uri = "WEB-INF/struts-logic.tld" prefix = "logic" %><html:html locale = "true">
<head>
<html:base/>
<title><bean:message key="userinfo.title"/></title>
</head><body background = "white">
<h3>user information</h3>
<table width="400" border="0" align="center">
<html:form action = "adduser.do" focus ="username" method = "GET"> <html:text property = "username" maxlength = "16"/>
<html:text property = "password" maxlength = "10"/> <html:submit property = "add"/></html:form>
</table></body>
</html:html>下面还有
package com.evertech.test;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;public final class UserinfoForm extends ActionForm
{ private String username;
private String password;
public void setusername(String username)
{
this.username = username;
} public String getusername()
{
return this.username;
} public void setpassword(String password)
{
this.password = password;
} public String getpassword()
{
return this.password;
} public void reset(ActionMapping actionmapping, HttpServletRequest httpservletrequest)
{
this.username = null;
this.password = null;
} public ActionErrors validate(ActionMapping actionmapping, HttpServletRequest httpservletrequest)
{
ActionErrors actionerrors = new ActionErrors();
if(username == null || username.length() < 1)
actionerrors.add("username", new ActionError("error.username.required"));
if(password == null || password.length() < 1)
actionerrors.add("password", new ActionError("error.password.required"));
return actionerrors;
}}
AddUserAction.java:package com.evertech.test;import java.sql.*;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.apache.struts.config.ActionConfig;public class AddUserAction extends Action
{ public static EverLog log = EverLog.getInstance(com.evertech.test.AddUserAction.class); public ActionForward execute(ActionMapping actionmapping,
ActionForm actionform,
HttpServletRequest request,
HttpServletResponse response)
{
org.apache.struts.util.MessageResources messageresources = getResources(request);
ActionErrors errors = new ActionErrors();
Connection connection;
PreparedStatement preparedstatement; String username = request.getParameter("username");
String password = request.getParameter("password");
try
{
Database database = new Database(); //自定义的一个连接数据库的类
connection = database.getConn(); //取得一个连结
String sql = "insert into userinfo( username,password ) values(?,?)";
preparedstatement = connection.prepareStatement(sql);
preparedstatement.setString(1, username);
preparedstatement.setString(2, password);
preparedstatement.executeUpdate();
}
catch(SQLException sqlexception)
{
log.debug("SQL execute Error:" + sqlexception);
errors.add("databaseerror", new ActionError("error.db.add"));
connection = null;
preparedstatement = null; }
finally
{
connection = null;
preparedstatement = null;
}
return actionmapping.findForward("success");
}}Database.java:
package com.evertech.test;import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;public class Database
{ public static EverLog log = EverLog.getInstance(com.evertech.test.Database.class);
public Connection getConn()
{
Connection connection;
try
{
InitialContext initialcontext = new InitialContext();
if(initialcontext == null)
throw new Exception("No Context");
Context context = (Context)initialcontext.lookup("java:/comp/env");
DataSource datasource = (DataSource)context.lookup("jdbc/myoracle");
connection = datasource.getConnection();
}
catch(Exception exception)
{
log.debug("database connetion exception:" + exception);
connection = null;
}
return connection;
}}createtable.sql:create table userinfo(
username varchar2(20) primary key,
password varchar2(10) not null
)全都贴出来了,盼解决,谢谢
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
NewForm newForm = (NewForm)form;
.......
type="com.evertech.test.AddUserAction"
attribute="userinfoForm"
scope="request"
input="/userinfo.jsp"
validate="true"> <forward name="success" path="/userinfo.jsp"/>
</action>
中attribute="userinfoForm"应该是name="userinfoForm"
name才是用来指定form-bean的
这个你可以看看strust-config.dtd里面的注释写得很明白还有request.getParameter这种方式也可以取到数据,并没有影响