struts1是WEB程序MVC分层架构中的C,属于控制层,主要进行处理用户的请求,基于请求驱动。
获取用户的请求地址并将表单中的数据封装到Form 对象后交给Action进行处理。
在Action中进行条用业务层处理具体的请求后将结果通过ActionMapping封装跳转地址返回给用户。
struts1是对servlet的再次封装,使得更加灵活高效。下面以一个登录的实例讲解struts的开发过程。
1.使用MyEclipse add Struts 1.X 会自动加入jar包和web.xml配置文件信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
简单demo 只需要创建一个Action
2.LoginAction
package com.yourcompany.struts.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.yourcompany.struts.form.LoginActionForm;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String path = "error";
LoginActionForm loginActionForm = (LoginActionForm) form;
System.out.println(loginActionForm);
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
if(null != userName && "admin".equals(userName) && null != passWord && "admin".equals(passWord)){
path = "success";
request.setAttribute("userName", userName);
}else{
path = "error";
}
return mapping.findForward(path);
}
}
3.struts-config.xml 配置内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
<action path="/login" type="com.yourcompany.struts.action.LoginAction" scope="request">
<forward name="success" path="/jsp/loginSuccess.jsp"></forward>
<forward name="error" path="/jsp/loginError.jsp"></forward>
</action>
</action-mappings>
</struts-config>
4.login页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'login.jsp' starting page</title>
</head>
<body>
<h1>登陆页面</h1>
<hr>
<form action="<%=path %>/login.do" method="post" >
userName:<input id="userName" name="userName" type="text" /><br>
passWord:<input id="passWord" name="passWord" type="password" /><br>
<input type="submit" id="submit" name="submit" value="submit" />
</form>
</body>
</html>
5.loginsuccess 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'loginSuccess.jsp' starting page</title>
</head>
<body>
<h1>欢迎[<%=request.getAttribute("userName") %>]
Success
</h1>
</body>
</html>
6.loginerror 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'loginError.jsp' starting page</title>
</head>
<body>
<h1>登陆失败了
</h1>
</body>
</html>
简单的不得了。熟悉一下,下个项目要用Struts1 二次开发。最基础的好简单。但是配置文件内容都好多好多。