今天把小练习昨完了,大致是一个用struts1框架写的一个注册后显示成功的小项目,并没有用到数据库啥的,感觉之前光看理论没有这么大的进步,果然动手才是学习编程最好的方法.今天买了几本计算机基础的书,感觉这些原来在学校感觉不重要的东西(也没有认真的去学习,多数是60万岁的思想)现在觉得当初是多么的浪费时间,书买回来以后晚上回家自己看看,多看看基础底层的东西,对以后应该会有很大的帮助的.最后把做的小练习放上来,请大家指点指点,页面的就不放了,太丑了^_^
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<!--name:表单名,规范,小写 -->
<!--type:表单全路径 -->
<form-bean name="RegisterForm" type="cn.licl.form.RegisterForm" />
</form-beans>
<action-mappings>
<!-- path:访问action的资源名-->
<!-- name:关联的表单名-->
<!-- type:该action的全路径名-->
<action path="/register" type="cn.licl.action.RegisterAction" name="RegisterForm">
<!-- name:Action里的forward的结果名-->
<!-- type:转发到的页面地址-->
<forward name="error" path="/error.jsp" />
<forward name="success" path="/success.jsp" />
<forward name="null" path="/null.jsp" />
</action>
</action-mappings>
</struts-config>
actionFrom:
package cn.licl.form;
import java.util.Arrays;
import org.apache.struts.action.ActionForm;
public class RegisterForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
// 用户名
String name;
// 第一次输入密码
String pwd1;
// 确认密码
String pwd2;
// 性别
String sex;
// 爱好,因为有四个,所以定义为数组
String[] hobby;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd1() {
return this.pwd1;
}
public void setPwd1(String pwd1) {
this.pwd1 = pwd1;
}
public String getPwd2() {
return this.pwd2;
}
public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getHobby() {
return this.hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "register [name=" + this.name + ", pwd1=" + this.pwd1
+ ", pwd2=" + this.pwd2 + ", sex=" + this.sex + ", hobby="
+ Arrays.toString(this.hobby) + "]";
}
public RegisterForm() {
super();
}
public RegisterForm(String name, String pwd1, String pwd2, String sex,
String[] hobby) {
super();
this.name = name;
this.pwd1 = pwd1;
this.pwd2 = pwd2;
this.sex = sex;
this.hobby = hobby;
}
}
action:
package cn.licl.action;
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 cn.licl.form.RegisterForm;
public class RegisterAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
RegisterForm registerForm = (RegisterForm) form;
// 得到用户输入的信息做if判断
String name = registerForm.getName();
String pwd1 = registerForm.getPwd1();
String pwd2 = registerForm.getPwd2();
// 如果用户输入的姓名,密码为空的话,跳转到null页面提醒不能为空
if (name == "" | pwd1 == "" | pwd2 == "") {
return mapping.findForward("null");
}
// 判断用户两次输入的密码是否相等
else if (pwd1.equals(pwd2)) {
request.setAttribute("registerMessage", registerForm);
return mapping.findForward("success");
} else {
// 密码输入有误跳到error
return mapping.findForward("error");
}
}
}