zoukankan      html  css  js  c++  java
  • Struts入门初体验

    一 ,入门小案例

    1,首先引入三个jar

            <dependency>
                <groupId>javaee</groupId>
                <artifactId>javaee-api</artifactId>
                <version>5</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.3.4.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts.xwork</groupId>
                <artifactId>xwork-core</artifactId>
                <version>2.3.4.1</version>
            </dependency>
    

      2,配置web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
    
      <filter>
        <filter-name>struts1</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts1</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    </web-app>
    

      3,创建实体类HelloAction并实现Action

    public class HelloAction implements Action{
    
        public String execute() throws Exception {
            return SUCCESS;
        }
    }
    

      4,创建配置文件struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
            <!DOCTYPE struts PUBLIC
                    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    
            <action name="hello" class="cn.happy.controller.HelloAction">
            <result name="success">index.jsp</result>
        </action>
            
        </package>
    </struts>
    

      5,入门小案例就写完啦,通过struts.xml中action的name名来访问。

    二 ,登陆小案例

    1,创建实体类LoginAction

        private  String username;   //用户名
        private  String password;   //登陆密码
        public String execute() throws Exception {
            //如果你的用户名和密码都是admin,return success.反之,走到login
        if(username.equals("admin")&&password.equals("admin")){
            return SUCCESS;
        }else {
            return LOGIN;
        }
     public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        }
    

      

    2,配置struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
            <!DOCTYPE struts PUBLIC
                    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
            <action name="login" class="cn.happy.controller.LoginAction">
                <result name="success">day01/success.jsp</result>
                <result name="login">day01/login.jsp</result>
            </action>
        </package>
    </struts>
    

    3,开发视图登陆页面

    <%@taglib prefix="s" uri="/struts-tags" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <s:form name="form1" namespace="/" method="post" action="login">
        请输入用户名:</br>  <s:textfield name="username"/>
        请输入密码:<s:password name="password"/>
        <s:submit value="登陆"></s:submit>
    </s:form>
    
    </body>
    </html>
    

     

    三,强化一下刚才的登陆案例,通过javaBean自动装配

    1,新建实体类UserInfo

    2,修改刚才的LoginAction页面

        //植入实体类
    private UserInfo info=new UserInfo();
    public String execute() throws Exception {
            //如果你的用户名和密码都是admin,return success.反之,走到login
        if(info.getUsername().equals("admin")&&info.getPassword().equals("admin")){ return SUCCESS; }else { return LOGIN; }
    public UserInfo getInfo() {
    return info;
    }

    public void setInfo(UserInfo info) {
    this.info = info;
    }
    }


    
    

      3,修改一下登陆页面

    <%@taglib prefix="s" uri="/struts-tags" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <s:form name="form1" namespace="/" method="post" action="login">
    请输入用户名:</br>  <s:textfield name="info.username"/>
    请输入密码:<s:password name="info.password"/>
    <s:submit value="登陆"></s:submit> </s:form> </body> </html>

      4,登陆成功后success.jsp

    5,这个时候登录名已经自动装配上了

    四,Struts2访问Servlet API

    解耦方案一:: 对Servlet API进行封装   ,借助ActionContext

     

    
    

  • 相关阅读:
    hdu 1695(莫比乌斯反演)
    hdu 6035(Colorful Tree)
    Splay模板(序列终结者)
    我的CSDN博客
    【洛谷P2041 分裂游戏】数学+瞎蒙
    【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导
    【LOJ 109 并查集】 并查集
    【洛谷P1816 忠诚】线段树
    【洛谷1339 [USACO09OCT]】热浪Heat Wave 图论+最短路
    【洛谷1433】吃奶酪 搜索+剪枝
  • 原文地址:https://www.cnblogs.com/spghs/p/8473841.html
Copyright © 2011-2022 走看看