那些年Struts 1.X 而今我仍是不會

struts1是WEB程序MVC分層架構中的C,屬於控制層,主要進行處理用戶的請求,基於請求驅動。html

獲取用戶的請求地址並將表單中的數據封裝到Form 對象後交給Action進行處理。java

在Action中進行條用業務層處理具體的請求後將結果經過ActionMapping封裝跳轉地址返回給用戶。
web

struts1是對servlet的再次封裝,使得更加靈活高效。下面以一個登陸的實例講解struts的開發過程。apache


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
app

2.LoginActionjsp

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 配置內容post

<?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頁面url

<%@ 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 頁面spa

<%@ 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 二次開發。最基礎的好簡單。可是配置文件內容都好多好多

相關文章
相關標籤/搜索