struts 1 流程
<?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">
<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>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<?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="com.days7.web.form.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" name="loginForm" scope="request" type="com.days7.web.LoginAction">
<forward name="success" path="/success.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
</struts-config>
login.jsp
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<br>
<form name="loginForm" method="post" action="${pageContext.request.contextPath}/login.do">
<table border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="aa"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="psw" value=""></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
</body>
</html>
LoginForm
package com.days7.web.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private String username;
private String psw;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
}
LoginAction
package cn.itcast.web.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.days7.web.form.LoginForm;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm loginForm=(LoginForm)form;
String username=request.getParameter("username");
if("sa".equals(loginForm.getUsername())){
return mapping.findForward("success");
}else{
return mapping.findForward("error");
}
}
}
服务启动后,载入web.xml,依据配置实例化ActionServlet,并载入struts-config.xml。当用户发出请求后(以*.do)结尾,ActionServlet截获请求并根据请求路径查找struts-config.xml中action的path。根据对应action中的name属性找到formbean,封装表单数据到formbean中;同时封装action中各个属性到actionmapping中;实例化type属性对应的action,执行execute方法,将封装好的frombean和actionmapping作为execute参数。