Structs1.3入门教程实例,附完整源代码

这是一个登录案例,使用了Structs1的框架,配置了很久,开发工具用的是IDEA.真的不容易,开始错了很多次,原因很简单,在写structs-config.xml的时候是直接粘贴的,没有一步一步的复制进去.具体原因应该是导致编译的时候没有找到对应的文件.不过终于成功了,与君共勉.

image

登录界面截图

image

登录结果截图

image

web.xml配置图

image

structs-config.xml配置图

image

RegisterForm源码截图

image

RegisterAction源码截图

image

附源代码

package com.lxgyx.web.struts1.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lxgyx.web.struts1.form.RegisterForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


//分控制器
public class RegisterAction extends Action{
//覆写execute()方法
@Override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

request.setCharacterEncoding("UTF-8");

//父子ActionForm强转
RegisterForm registerForm = (RegisterForm)form;
String username = registerForm.getUsername();
String password = registerForm.getPassword();



return mapping.findForward("toSuccessJsp");

//return new ActionForward("/success.jsp",false);

}
}


----------------------------------

package com.lxgyx.web.struts1.form;

import org.apache.struts.action.ActionForm;

public class RegisterForm extends ActionForm {
public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

private String username;
private String password;

public void setUsername(String username) {
this.username = username;
//System.out.println("框架自动注入");
}

public void setPassword(String password) {
this.password = password;
//System.out.println("框架自动注入");
}
}

张 大鹏

北京理想国惠玩科技有限公司CEO

发表评论

电子邮件地址不会被公开。 必填项已用*标注