Eclipse开发Struts应用
一、开发步骤
1、新建web项目
2、为项目添加Struts类库及文件
3、新建FormBean
4、新建Action
5、新建Forward
6、编辑Action
7、部署运行项目
二、新建web项目
用Eclipse开发Struts应用图文步骤_Struts
三、为项目添加Struts类库及文件
用Eclipse开发Struts应用图文步骤_Struts_02
 
l    Struts conifg pathStruts Config文件的位置
l    Struts specificationStruts版本
l    ActionServlet nameAction Servlet的名字,默认就可以
l    URL patten:用默认*.do
l    Base package for new classesStruts生成的FormBeanAction的包名
l    Defautl application resources:指定资源文件ApplicationResource的全名,包括包名
 
四、新建FormBean
用Eclipse开发Struts应用图文步骤_职场_03
l    NameFormBean的名字
l    Form ImplFormBean的类型,这里选择Dynamic FormBean,则通过struts-config.xml文件动态帮且我们生成FromBean文件
l    Form PropertiesFormBean的属性,这里有username,password两个属性
 
五、新建Action
 
用Eclipse开发Struts应用图文步骤_Struts_04
 
l    use case:一般取完成某功能的单词
l    path:调用当前Action的路径
l    SupperclassAction的父类
l    TypeAction的全名
l    Form-Name:对应的FormBean名称
l    Form-ScopeFormBean的作用范围
l    Form-Validate Form:是否进行效验
l    Input Source:效验错误后回到哪个页面
 
六、新建Forward
 
用Eclipse开发Struts应用图文步骤_职场_05
 
l    Forward ScopeForward的作用范围,是全局转向还是局部转向
l    NameForward的名字
l    PathForward转向的页面
l    Redirect:选择Redirect转向
 
七、连接Forwad
可以用连线连接起来,如下图:
 
用Eclipse开发Struts应用图文步骤_休闲_06
 
对应的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>    
    <!-- 对FormBean的定义 -->    
    <form-beans>    
        <!-- FormBean的名称和类型,这里是动态FromBean -->    
        <form-bean name="loginForm"    
            type="org.apache.struts.action.DynaActionForm">    
            <!-- FormBean中的属性 -->    
            <form-property name="username" type="java.lang.String" />    
            <form-property name="password" type="java.lang.String" />    
        </form-bean>    
    </form-beans>    
    <global-exceptions />    
    <!-- 以下为两个全局的forward转向 -->    
    <global-forwards>    
        <forward name="success" path="/form/main.jsp" redirect="true" />    
        <forward name="fail" path="/form/fail.jsp" redirect="true" />    
    </global-forwards>    
    <!-- action的定义 -->    
    <action-mappings>    
        <!-- attribute:属性 input:对应输入的JSP文件 name:Action的名称    
            path:Action的访问路径 scope:Action的作用范围 type:Action类的全名
-->    
        <action attribute="loginForm" input="login.jsp" name="loginForm"    
            path="/login" scope="request"    
            type="com.meixin.struts.action.LoginAction" />    
    </action-mappings>    
    <message-resources    
        parameter="com.meixin.struts.ApplicationResources" />    
</struts-config>    
 
八、编辑Action
只需要修改execute()方法
 
 
package com.meixin.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 org.apache.struts.action.DynaActionForm;

public class LoginAction extends Action
{
  String username = "";
  String password = "";

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
  {
    // 通过loginForm可以自动地得到用户名和密码
    DynaActionForm loginForm = (DynaActionForm) form;
    // 获取用户名,返回Object类型,需要强制转换
    username = (String) loginForm.get("username");
    // 获取密码
    password = (String) loginForm.get("password");
    if (username.equals("meixin") && password.equals("123456"))
    {
      // 若合法则转向success对应的页面
      return mapping.findForward("success");
    }
    else
    {
      // 若不合法,则转向fail所对应的页面
      return mapping.findForward("fail");
    }
  }
}
 
九、对应的JSP页面
1、login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>    
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    
<html>    
  <head>
    <title>JSP for DynaActionForm form</title>
  </head>
  <body>
    <!-- 这里要输出处理表单的action访问路径 -->
    <html:form action="/login.do">        
      username : <html:text property="username"/><html:errors property="username"/><br/>
      password : <html:password property="password"/><html:errors property="password"/><br/>
      <html:submit/><html:cancel/>
    </html:form>
  </body>
</html>
2、main.jsp
<%@ 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 'main.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>
        ${username },欢迎您!<br>
    </body>
</html>
3、fail.jsp
<%@ 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 'fail.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>
        对不起,你输入的用户名和密码不正确!<a href="form/login.jsp">请重新输出!</a><br>
        
    </body>
</html>
十、输出结果
1、项目结构
用Eclipse开发Struts应用图文步骤_休闲_07
2、登录页面
用Eclipse开发Struts应用图文步骤_休闲_08
3、转向后的页面
用Eclipse开发Struts应用图文步骤_Struts_09