Struts入门案例

2014-11-24 07:14:32 · 作者: · 浏览: 0

login.jsp-->



u:

p:




LoginAction.java--->

public class LoginAction extends Action {
//我们需要重新编写一个方法 :execute会被自动调用,有点类似servlet里面的service方法
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//form转成对应的UserForm对象
UserForm userForm=(UserForm)form;
System.out.println("用户名-->"+userForm.getUsername());
System.out.println("密码-->"+userForm.getPassword());
request.setAttribute("username",userForm.getUsername());
if("123".equals(userForm.getPassword())){
return mapping.findForward("ok");
}else{
return mapping.findForward("err");
}
}
}


UserForm.java--->

package com.cb.forms;
//这是一个用户表单,用于填充数据
import org.apache.struts.action.ActionForm;
//继承一个超类ActionForm
public class UserForm extends ActionForm {
//定义属性【这里有一个规范,就是我们定义属性名字的时候,
//应该和jsp页面空间名称一样】
private String username;
private String password;
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;
}
}


struts-config.xml--->






















web.xml----->




action
org.apache.struts.action.ActionServlet


config
/WEB-INF/struts-config.xml



This is the description of my J2EE component
This is the display name of my J2EE component
test
test


action
*.do


test
/servlet/test


index.jsp