Struts中静态动态ActionForm的使用(一)
目录--------------------------------------------------------------------------------
登录例子
区别
时间属性
验证机制
--------------------------------------------------------------------------------
在Struts1中,我们经常接触ActionForm为静态ActionForm。静态是指开发者在使用struts框架时必须写对应的ActionForm。
在介绍动态ActionForm使用之前,我们回忆struts常用的ActionForm的方式,以便做对比。
举个完整的登录例子。
第一:配置web.xml
[html]
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> 第二:新建jsp页面(在login文件夹)
[html] view plaincopyprint?
<form action="../login.do" method="get">
用户名: <input name="username" type="text"><br>
密 码: <input name="password" type="text"><br>
<input type="submit" >
</form>
<form action="../login.do" method="get">
用户名: <input name="username" type="text"><br>
密 码: <input name="password" type="text"><br>
<input type="submit" >
</form>
第三:新建ActionForm
[html]
package com.login;
import java.util.Date;
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;
import org.apache.struts.action.ActionMessages;
public class LoginForm extends ActionForm{
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
}
package com.login;
import java.util.Date;
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;
import org.apache.struts.action.ActionMessages;
public class LoginForm extends ActionForm{
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
} 第四:新建Action
[html]
package com.login;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.action.DynaActionForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LoginForm loginForm=(LoginForm)form;
boolean flag=false;
if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){
flag=true;
}
request.setAttribute("username",loginForm.getUsername());
if(flag)
{
return mapping.findForward("success");
}else{
return mapping.findForward("false");
}  
补充:Web开发 , Jsp ,
- 更多JS疑问解答:
- 几个验证11位手机号码格式的js代码
- js把图片转换成 base64代码
- js把base代码转换成图片
- JS 将 base64编码的图片转化为图片文件
- js中的定时器
- js如何获得FCKeditor控件的值
- 用js限制投票的cookie .目前设置的为:<input type="" class="" onclick="'window.location...
- JS验证,这块“牛皮”反复修改都不能实现
- 在JS中使用DOM模型
- 如何用JS 获取本地文件夹的文件列表
- js中new 了两个Object数组。怎么样将数组内容合并,重复的内容?
- 求实现自动生成图片缩略图的JS代码
- JS脚本网页问题
- js,代码中"object"和"Object"区别?
- js+flash实现网页图片切换效果,出现边框,单击激活此控件。