---
title: "全自动化开发struts"
author: "cakin24"
date: "2017-10-28T18:52:45Z"
tags: []
categories: ["编程语言"]
slug: "struts"
draft: false
lastmod: "2017-10-28T19:00:02Z"
id: 2397923
url: "https://cakin24.iteye.com/blog/2397923"
---

一 创建一个web工程

 

二 引入struts开发包

 

三 开发login.jsp文件

&lt;%@ page language="java" import="java.util.\*" pageEncoding="ISO-8859-1"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;form action="/strutstool2/login.do" method="post"&gt;

        u:&lt;input type="text" name="username"&gt;&lt;br/&gt;

        p:&lt;input type="password" name="password"&gt;&lt;br/&gt;

        &lt;input type="submit" value="login"&gt;

    &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

四 开发action和actionForm

1、创建actionForm截图

![](http://dl2.iteye.com/upload/attachment/0127/5924/1529c38f-1d68-343b-bad2-6e5a67266564.png)  
 

创建完成后在struts-config.xml自动生成下面代码

  &lt;form-beans &gt;

    &lt;form-bean name="userForm" type="com.cakin.struts.form.UserForm" /&gt;

  &lt;/form-beans&gt;

在UserForm.java中自动生成下面代码

/*

\* Generated by MyEclipse Struts

\* Template path: templates/java/JavaClass.vtl

\*/

package com.cakin.struts.form;

import org.apache.struts.action.ActionForm;

/\**

\* MyEclipse Struts

\* Creation date: 10-02-2017

\*

\* XDoclet definition:

\* @struts.form name="userForm"

\*/

public class UserForm extends ActionForm {

    /*

     * Generated fields

     \*/

    /\** username property \*/

    private String username;

    /\** password property \*/

    private String password;

    /*

     * Generated Methods

     \*/

    /\**

     * Returns the username.

     * @return String

     \*/

    public String getUsername() {

        return username;

    }

    /\**

     * Set the username.

     * @param username The username to set

     \*/

    public void setUsername(String username) {

        this.username = username;

    }

    /\**

     * Returns the password.

     * @return String

     \*/

    public String getPassword() {

        return password;

    }

    /\**

     * Set the password.

     * @param password The password to set

     \*/

    public void setPassword(String password) {

        this.password = password;

    }

}

2、创建action截图

![](http://dl2.iteye.com/upload/attachment/0127/5926/01c4829e-7b66-3fdf-a00c-6367dcd69868.png)  
 

创建完成后在struts-config.xml自动生成下面代码

  &lt;action-mappings &gt;

    &lt;action

      attribute="userForm"

      name="userForm"

      path="/login"

      scope="request"

      type="com.cakin.struts.action.LoginAction"

      validate="false"

      cancellable="true" /&gt;

 

  &lt;/action-mappings&gt;

创建完成后自动生成LoginAction

/*

\* Generated by MyEclipse Struts

\* Template path: templates/java/JavaClass.vtl

\*/

package com.cakin.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.cakin.struts.form.UserForm;

/\**

\* MyEclipse Struts

\* Creation date: 10-02-2017

\*

\* XDoclet definition:

\* @struts.action path="/login" name="userForm" scope="request"

\*/

public class LoginAction extends Action {

    /*

     * Generated Methods

     \*/

    /\**

     * Method execute

     * @param mapping

     * @param form

     * @param request

     * @param response

     * @return ActionForward

     \*/

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response) {

        UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

        return null;

    }

}

 

五 编写ok.jsp和err.jsp

1 ok.jsp

&lt;%@ page language="java" import="java.util.\*" import="com.cakin.struts.form.\*" pageEncoding="utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'wel.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    welcome &lt;%=((UserForm)request.getAttribute("userForm")).getUsername() %&gt; &lt;br&gt;

  &lt;/body&gt;

&lt;/html&gt;

2 err.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="ISO-8859-1"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'err.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    err&lt;br&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

六 配置跳转关系

![](http://dl2.iteye.com/upload/attachment/0127/5928/ea7f9190-14b1-3a7a-88ff-320c107c94e5.png)  
 

 

七 在atcion中添加业务逻辑

        public ActionForward execute(ActionMapping mapping, ActionForm form,

                        HttpServletRequest request, HttpServletResponse response) {

                UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

                

                if("123".equals(userForm.getPassword())){

                        //把名字存放到request对象域

                        //request.setAttribute("username", userForm.getUsername());

                        return mapping.findForward("ok");

                }

                else{

                        return mapping.findForward("err");

                }

        }

 

八 测试

![](http://dl2.iteye.com/upload/attachment/0127/5930/8f512407-f38b-3f11-930a-6f1d882b8762.png)