Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 326|回复: 0

[struts学习]Struts2_0 My First Example

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 06:15:38 | 显示全部楼层 |阅读模式
    1. folder structure src
      |--sturts.xml
      |--struts.property
      |--user
             |-- user.xml
             |-- User.java
             |-- UserAction.java
             |-- BizService.java
    WebContent
      |-- user
             |-- index.jsp
             |-- UserForm.jsp
             |-- UserList.jsp
      |-- WEB-INF
             |-- web.xml
             |-- lib
                   |-- ... (All the need jar packages of Struts2.0)
       
       
       
         
       

         
       
      


    2. struts.xml
      
      
       
       <!
       DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd"
       >
       


       <
       struts
       >
       

       
       <
       include
       file
       ="user/user.xml"
       
       />
       


       </
       struts
       >
       
      
    3. struts.property
    Notice: If you have config these two constants in struts.xml, you shouldn"t config them here any more, or there will be errors report.
      
      
       
       struts.devMode
       =
        true
    struts.enable.DynamicMethodInvocation
       =
        true
       
      
    4. user.xml
      
      
       
       <!
       DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd"
       >
       


       <
       struts
       >
       

       
       <
       package
       name
       ="user"
        namespace
       ="/user"
        extends
       ="struts-default"
       >
       
            
            
       <
       action
       name
       ="crud!*"
        method
       ="{1}"
        class
       ="user.UserAction"
       >
       
                
       <
       result
       name
       ="input"
       >
       /user/UserForm.jsp
       </
       result
       >
       
                
       <
       result
       name
       ="list"
       >
       /user/UserList.jsp
       </
       result
       >
       
                
       <
       result
       name
       ="success"
        type
       ="redirect-action"
       >
       crud!list.action
       </
       result
       >
       
            
       </
       action
       >
       

       
       </
       package
       >
       

       </
       struts
       >
       
      
    5. User.java
      
      
       
       package
        user;


       public
       
       class
        User
       ...
       {

        private String userid;
        private String username;
        private String password;
       
        /** *//**
         * @return the password
         */
        public String getPassword() ...{
            return password;
        }
        /** *//**
         * @param password the password to set
         */
        public void setPassword(String password) ...{
            this.password = password;
        }
        /** *//**
         * @return the userid
         */
        public String getUserid() ...{
            return userid;
        }
        /** *//**
         * @param userid the userid to set
         */
        public void setUserid(String userid) ...{
            this.userid = userid;
        }
        /** *//**
         * @return the username
         */
        public String getUsername() ...{
            return username;
        }
        /** *//**
         * @param username the username to set
         */
        public void setUsername(String username) ...{
            this.username = username;
        }

    }
       
      
    6. UserAction.java
      
      
       
       package
        user;


       import
        java.util.ArrayList;

       import
        java.util.List;


       import
        com.opensymphony.xwork2.ActionSupport;


       public
       
       class
        UserAction
       extends
        ActionSupport
       ...
       {
       
        private User user = new User();
       
        private List<User> users;
       
        public String list() ...{
            BizService bizService = new BizService();
            users = bizService.getUsers();
            
            return "list";
        }
       
        public String add() ...{
            BizService bizService = new BizService();
            if(bizService.add(user)) ...{
                return SUCCESS;
            } else ...{
                return INPUT;
            }
        }
       
        /** *//**
         * @return the users
         */
        public List getUsers() ...{
            return users;
        }

        /** *//**
         * @param users the users to set
         */
        public void setUsers(List<User> users) ...{
            this.users = users;
        }
       
        /** *//**
         * @return the user
         */
        public User getUser() ...{
            return user;
        }

        /** *//**
         * @param user the user to set
         */
        public void setUser(User user) ...{
            this.user = user;
        }
       
    }
       
      
    7. BizService.java
    This is just a example, but does not do actual work indeed.
      
      
       
       package
        user;


       import
        java.util.ArrayList;

       import
        java.util.List;


       public
       
       class
        BizService
       ...
       {
        public List getUsers() ...{
            List<User> alUser = new ArrayList();
            
            User u1 = new User();
            u1.setUserid("1");
            u1.setUsername("lwp1");
            u1.setPassword("password1");
            
            User u2 = new User();
            u2.setUserid("2");
            u2.setUsername("lwp2");
            u2.setPassword("password2");
            
            alUser.add(u1);
            alUser.add(u2);
            
            return alUser;
        }
       
        public boolean add(User user) ...{
            //Do actual addition here
            return true;
        }
    }
          

       
      
    8. index.jsp
    Two links: one used to show list action, and the other to show add action.
      
      
       
       
       <%
       ...
       @ page contentType="text/HTML; charset=UTF-8"
       %>
       

       <%
       ...
       @ taglib prefix="s" uri="/struts-tags"
       %>
       

       <%
       ...
       --
    @ author: Newweapon
    @ date: 2007-5-4, ??02:47:36
    --
       %>
       

       <
       html
       >
       

       <
       head
       >
       

       <
       title
       >
        Struts 2.0
       </
       title
       >
       

       </
       head
       >
       

       <
       body
       >
       

       <
       p
       >
          
       <
       a
       href
       ="<s:url action="
       crud!list"
       />
       "> list users
       </
       a
       >
       
       </
       p
       >
       

       <
       p
       >
       
       <
       a
       href
       ="UserForm.jsp"
       >
        add a user
       </
       a
       >
       
       </
       p
       >
       

       </
       body
       >
       

       </
       html
       >
       
      
    9. UserForm.jsp
      
      
       
       
       <%
       ...
       @ page contentType="text/html; charset=UTF-8"
       %>
       

       <%
       ...
       @ taglib prefix="s" uri="/struts-tags"
       %>
       

       <%
       ...
       --
    @ author: Newweapon
    @ date: 2007-5-4, ??06:20:09
    --
       %>
       

       <
       html
       >
       

       <
       head
       >
       

       <
       title
       >
        User Form
       </
       title
       >
       

       </
       head
       >
       

       <
       body
       >
       

       <
       h2
       >
       User Form
       </
       h2
       >
       

       <
       table
       >
       

       <
       s:form
       action
       ="crud!add.action"
        method
       ="post"
       >
       
       
       <
       s:textfield
       name
       ="user.userid"
        value
       ="%{user.userid}"
        label
       ="UserID"
        size
       ="20"
       
       />
       
       
       <
       s:textfield
       name
       ="user.username"
        value
       ="%{user.username}"
        label
       ="UserName"
        size
       ="20"
       
       />
       
       
       <
       s:textfield
       name
       ="user.password"
        value
       ="%{user.password}"
        label
       ="Password"
        size
       ="20"
       
       />
       
       
       <
       s:submit
       value
       ="submit"
       
       />
       

       </
       s:form
       >
       

       </
       body
       >
       

       </
       html
       >
       
      
    10. UserList.jsp
      
      
       
       
       <%
       ...
       @ page contentType="text/html; charset=UTF-8"
       %>
       

       <%
       ...
       @ taglib prefix="s" uri="/struts-tags"
       %>
       

       <%
       ...
       --
    @ author: Newweapon
    @ date: 2007-5-4, pm 03:11:41
    --
       %>
       

       <
       html
       >
       

       <
       head
       >
       

       <
       title
       >
        User List
       </
       title
       >
       

       </
       head
       >
       

       <
       body
       >
       

       <
       h2
       >
       User List
       </
       h2
       >
       

       <
       table
       >
       

       <
       tr
       >
       
       
       <
       td
       >
       UserID
       </
       td
       >
       
       
       <
       td
       >
       UserName
       </
       td
       >
       
       
       <
       td
       >
       Password
       </
       td
       >
       

       </
       tr
       >
       

       <
       s:iterator
       value
       ="users"
        status
       ="status"
       >
       

       <
       tr
       >
       
       
       <
       td
       ><
       s:property
       value
       ="userid"
       
       /></
       td
       >
       
       
       <
       td
       ><
       s:property
       value
       ="username"
       
       /></
       td
       >
       
       
       <
       td
       ><
       s:property
       value
       ="password"
       
       /></
       td
       >
       

       </
       tr
       >
       

       </
       s:iterator
       >
       

       </
       table
       >
       

       <
       p
       ><
       a
       href
       ="index.jsp"
       >
       Back to index.jsp
       </
       a
       ></
       p
       >
       

       </
       body
       >
       

       </
       html
       >
       
      
    11. web.xml
      
      
       
       <?
       xml version="1.0" encoding="UTF-8"
       ?>
       

       <
       web-app
       id
       ="WebApp_9"
        version
       ="2.4"
        xmlns
       ="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi
       ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
       ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
       >
       

       
       <
       display-name
       >
       ebookhouse
       </
       display-name
       >
       
       
       
       <
       filter
       >
       
            
       <
       filter-name
       >
       struts2
       </
       filter-name
       >
       
            
       <
       filter-class
       >
       org.apache.struts2.dispatcher.FilterDispatcher
       </
       filter-class
       >
       
       
       </
       filter
       >
       
       
       <!--
        The webapplication"s descriptor file contains one filter and its mapping.
               By default, the filter is mapped to /*, meaning all requests will be
               intercepted, but only those ending with a specific suffix (.action,
               by default) and certain special paths (for static files) will be processed
               and handled by WebWork
         
       -->
       
       
       <
       filter-mapping
       >
       
            
       <
       filter-name
       >
       struts2
       </
       filter-name
       >
       
            
       <
       url-pattern
       >
       /*
       </
       url-pattern
       >
       
       
       </
       filter-mapping
       >
       

       
       <
       listener
       >
       
            
       <
       listener-class
       >
       org.springframework.web.context.ContextLoaderListener
       </
       listener-class
       >
       
       
       </
       listener
       >
       
       
       
       <
       welcome-file-list
       >
       
            
       <
       welcome-file
       >
       index.html
       </
       welcome-file
       >
       
            
       <
       welcome-file
       >
       index.htm
       </
       welcome-file
       >
       
            
       <
       welcome-file
       >
       index.jsp
       </
       welcome-file
       >
       
       
       </
       welcome-file-list
       >
       

       </
       web-app
       >
       
      
    12. OK. Now input http://localhost:8080/yoursite/user/index.jsp into your browser and enjoy it!
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 07:31 , Processed in 0.373363 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表