Struts1框架有五个常用对象:Actionservlet、Actionmapping、Actionform、Action、Actionforward
a) Actionservlet是核心控制,URL 地址映射、ActionForm的匹配、 Action 的执行都需要这个类来进行导航
b) Actionmapping:它们可将请求URI映射到Action类,并且将Action类与ActionFormbean相关联
c) Actionform:收集表单数据
d) Action: 完成所需的业务逻辑,确定要跳转的页面
e) Actionforward:用来封装转发路径的
info程序文件构成一览:
1. 新建一个Web工程,向WebRoot/WEB-INF/lib中copystruts1的jar包:
antlr-2.7.2.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-digester-1.8.jar
commons-logging-1.0.4.jar
commons-validator-1.3.1.jar
oro-2.0.8.jar
struts-core-1.3.10.jar
struts-taglib-1.3.10.jar
struts-tiles-1.3.10.jar
2. 配置文件:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<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>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="form" type="com.tao.form.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" type="com.tao.action.LoginAction" name="form">
<forward name="success" path="/Success.jsp"></forward>
<forward name="failure" path="/Failure.jsp"></forward>
</action>
</action-mappings>
</struts-config>
struts1.x是一种MVC框架。
MVC: Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写
LoginAction →控制器(controller)
LoginForm →模型(model)
*.jsp → 视图(view)
LoginAction.java
package com.tao.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 com.tao.form.LoginForm;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm f = (LoginForm) form;
String username = f.getUsername();
String password = f.getPassword();
System.out.println(username);
if (username != null && username.equals("amaker")) {
return mapping.findForward("success");
} else {
return mapping.findForward("failure");
}
}
}
package com.tao.form;
import org.apache.struts.action.ActionForm;
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;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="<%=path%>/login.do"
method="post">
<table border="0">
<tr>
<td>Userame:</td>
<td><input type="textbox" name="username" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="auto0" value="Login" />
</td>
</tr>
</table>
</form>
<hr>
</body>
</html>
Success.jsp和Failure.jsp只是验证用的测试页面,就不贴代码了。