1 准备ActionForm与Acion 的java文件
LoginAction.java:
package myAction;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import myActionForm.LoginForm;
public class LoginAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
String str = "";
LoginForm login = (LoginForm)form;
if(login.getUsername().equals("") || login.getPassword().equals("") || login.getUsername() == null || login.getPassword()==null){
request.setAttribute("message", "no user or no passwd");
str = "login";
}else if(login.getUsername().equals("bobo") && login.getPassword().equals("123456")){
str = "success";
}else{
str = "login";
}
return mapping.findForward(str);
}
}
LoginForm.java 并添加 validate代码
package myActionForm;
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;
public class LoginForm extends ActionForm {
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;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors = new ActionErrors();
if(username == "" || password == ""){
errors.add("name",new ActionMessage("hello.error.name"));
}
return errors;
}
}
2 准备View层的 login.jsp (登录页面)success.jsp(成功跳转页面)
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String mess = (String)request.getAttribute("message");
if(mess != null){
out.println("<script language = 'javascript'>alert('"+mess+"')</script>");
}
%>
<form action="login.do" method="get">
username:<input type="text" name="username"><br/>
password:<input type="password" name="password"><br/>
<input type="submit" value = "submit">
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
congratulation!
</body>
</html>
3 配置文件
web.xml用通用的配置文件
struts-config.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginform" type="myActionForm.LoginForm"></form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<action-mappings>
<action name="loginform" //与loginform保持一致
type="myAction.LoginAction" //指定类
path="/login" // 指定action的path注意与表单 action保持一致
input="/login.jsp">// 输入的页面
<forward name="success" path="/success.jsp"></forward>
<forward name="login" path="/login.jsp"></forward>
</action>
<!-- sample input and input submit actions
<action
path="/Input"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/Input.jsp"/>
<action
path="/InputSubmit"
type="app.InputAction"
name="inputForm"
scope="request"
validate="true"
input="/pages/Input.jsp"/>
<action
path="/edit*"
type="app.Edit{1}Action"
name="inputForm"
scope="request"
validate="true"
input="/pages/Edit{1}.jsp"/>
end samples -->
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
PS调试过程:
期间报出 404的错误:
HTTP Status 404 - Servlet action is not available
type Status report
message Servlet action is not available
description The requested resource is not available.
反复查看配置,结果是 plug-in配置了多余的表单校验。
(提醒,千万注意,不要按照有的微博上的介绍,把 plug-in引入(引入validate xml的引入)。害的自己check 配置半天,结果是发现plug_in配置的错误!)