一 创建一个web工程
 
二 引入struts开发包
 
三 开发login.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
   
        <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>
    <form action="/strutstool2/login.do" method="post">
        u:<input type="text" name="username"><br/>
        p:<input type="password" name="password"><br/>
        <input type="submit" value="login">
    </form>
  </body>
</html>
 
四 开发action和actionForm
1、创建actionForm截图

全自动化开发struts
 
创建完成后在struts-config.xml自动生成下面代码
  <form-beans >
    <form-bean name="userForm" type="com.cakin.struts.form.UserForm" />
  </form-beans>
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截图

全自动化开发struts
 
创建完成后在struts-config.xml自动生成下面代码
  <action-mappings >
    <action
      attribute="userForm"
      name="userForm"
      path="/login"
      scope="request"
      type="com.cakin.struts.action.LoginAction"
      validate="false"
      cancellable="true" />
 
  </action-mappings>
创建完成后自动生成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
<%@ page language="java" import="java.util.*" import="com.cakin.struts.form.*" 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 'wel.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>
    welcome <%=((UserForm)request.getAttribute("userForm")).getUsername() %> <br>
  </body>
</html>
2 err.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'err.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>
    err<br>
  </body>
</html>
 
六 配置跳转关系

全自动化开发struts
 
 
七 在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");
                }
        }
 
八 测试

全自动化开发struts
 

相关文章:

  • 2022-01-10
  • 2021-08-01
  • 2021-10-30
  • 2022-02-20
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2021-10-02
  • 2021-06-21
  • 2021-11-29
  • 2022-01-14
  • 2021-12-14
  • 2021-12-13
相关资源
相似解决方案