<!-- ========================= Tiles plugin ===============================--> <!-- This plugin initialize Tiles definition factory. This later can takes some parameters explained here after. The plugin first read parameters from web.xml, thenoverload them with parameters defined here. All parameters are optional. The plugin should be declared in each struts-config file. - definitions-config: (optional) Specify configuration file names. There can be several comma separated file names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if the Tiles definition factory is module aware. If true (default), there will be one factory for each Struts module. If false, there will be one common factory for all module. In this later case, it is still needed to declare one plugin per module. The factory will be initialized with parameters found in the first initialized plugin (generally the one associated with the default module). true : One factory per module. (default) false : one single shared factory for all modules - definitions-parser-validate: (optional) Specify if xml parser should validate the Tiles configuration file. true : validate. DTD should be specified in file header (default) false : no validation Paths found in Tiles definitions are relative to the main context. --> <plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> </plug-in>
* LoginForm.java
*
* Created on 2007年1月13日, 下午7:04
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.card;
import com.card.LoginAction;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
*
* @author haha
*/
public class LoginForm extends ActionForm{
private String cardnumber = null;
private String cardpassword = null;
public String getCardNumber() {return cardnumber;}
public String getCardPassword() {return cardpassword; }
public void setCardNumber(String cardnumber) {this.cardnumber = cardnumber; }
public void setCardPassword(String cardpassword) { this.cardpassword = cardpassword; }
public void reset(ActionMapping mapping, HttpServletRequest request) {
cardpassword = null;
cardnumber = null;
}
/** Creates a new instance of LoginForm */
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((cardnumber == null) || (cardnumber.length() < 1))
errors.add("cardnumber", new ActionMessage("error.cardnumber.required"));
if ((cardpassword == null) || (cardpassword.length() < 1))
errors.add("cardpassword", new ActionMessage("error.cardpassword.required"));
return errors;
}
}LoginAction/*
* LoginAction.java
*
* Created on 2007年1月13日, 下午7:35
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.card;
import com.card.DB;
import com.card.LoginForm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
/**
*
* @author haha
*/
public final class LoginAction extends Action{ private String S;
/** Creates a new instance of LoginAction */
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// Locale locale = getLocale(request);
// MessageResources messages = getResources(request);
// LoginForm frm = new LoginForm() ;
String cardnumber = ((LoginForm)form).getCardNumber();
String cardpassword = ((LoginForm)form).getCardPassword();
// SessionContainer exitingContainer = null;
HttpSession session = request.getSession();
session.setAttribute("cardnumber",cardnumber); try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:card");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//ResultSet rs = stmt.executeQuery("select * from cardinfo ");
ResultSet rs = stmt.executeQuery(
"select * from cardinfo where cardnumber='"+cardnumber+"'and cardpassword='"+cardpassword+"'");
while(rs.next()){
S=rs.getString("cardnumber");
}
rs.close();
stmt.close();
con.close();
// DB db = new DB();
// S = db.login(cardnumber,cardpassword);
}catch(Exception ex){}
if(S == null)
return (mapping.findForward("loginsucess"));
else return (mapping.findForward("loginfail"));}
}
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<!-- <form-bean name="LoginAction" type="com.card.LoginAction"/> -->
<form-bean name="LoginForm" type ="com.card.LoginForm" />
<form-property name="cardnumber" type="java.lang.String"/>
<form-property name="cardpassword" type="java.lang.String"/>
</form-beans>
<global-exceptions>
</global-exceptions> <global-forwards>
<forward name="welcome" path="/Welcome.do"/>
<forward name="login" path="/login.jsp"/>
<forward name="loginsucess" path ="index.jsp"/>
<forward name="loginfail" path ="index.jsp"/>
</global-forwards> <action-mappings>
<action path="/Welcome" forward="/main.jsp"/>
<!-- <action path="/login" forward="/main.jsp"/> -->
<action path="/login"
type = "com.card.LoginAction"
name = "LoginForm"
scope= "request"
input ="/login.htm">
<forward name="loginsucess" path="index.jsp"/>
<forward name="loginfail" path="index.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="com/myapp/struts/ApplicationResource"/>
<!-- ========================= Tiles plugin ===============================-->
<!--
This plugin initialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
later case, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initialized plugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation Paths found in Tiles definitions are relative to the main context.
-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
private String cardpassword = null;
public String getCardNumber() {return cardnumber;}
public String getCardPassword() {return cardpassword; }
public void setCardNumber(String cardnumber) {this.cardnumber = cardnumber; }
public void setCardPassword(String cardpassword) { this.cardpassword = cardpassword; }楼上的说的没错,大小写敏感的,应该是getCardnumber和getCardpassword,set方法也一样
但是为什么是getCardnumber() 而不是getCardNumber() 呢,我还是不明白.
比如你自己定义属性变量
private String cardNumber;
那你的get方法就是getCardNumber了
因为Java本身是大小写敏感的,会认做是别的变量的,而且这种感觉不会很明显提示你说“找不到属性”这种错误,感觉也是struts一个缺陷吧而且你的属性第一个字母不要写成大写比较好,也会认不到的