zoukankan      html  css  js  c++  java
  • struts2 helloworld

    1. 新建项目: MyStruts2

    2.导入相应的jar包:

    3.修改web.xml文件:

    复制代码
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    复制代码

    小插曲:刚开始把filter写成了servlet,导致一直报错,后来才发现写错了,所以写的时候要细心

    4.在src下新建: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>
        <package name="helloworld" extends="struts-default">
            <action name="product-input">
                <result>/input.jsp</result>
            </action>
        </package>
    </struts>
    复制代码

    5.新建:index.jsp,主要代码如下:

    复制代码
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <a href="product-input.action">product-input</a>
    </body>
    </html>
    复制代码

    6.新建:input.jsp,主要代码如下:

    复制代码
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        input.jsp
    </body>
    </html>
    复制代码

    7.将项目部署到tomcat上并启动

    8.在浏览器地址栏:http://localhost:8080/MyStrust2/

    9.点击链接进入后结果如下:

    10.项目结构如下:

    ================================================== 

    11. 修改input.jsp,修改后如下:

    复制代码
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="product-save">
            username:<input type="text" name="name"><br>
            password:<input type="text" name="password"><br>
            <input type="submit" value="login">
        </form>
    </body>
    </html>
    复制代码

    12.修改struts.xml,添加action(product-save) ,修改后如下:

    复制代码
    <?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>
        <package name="helloworld" extends="struts-default">
            <action name="product-input">
                <result>/input.jsp</result>
            </action>
            
            <action name="product-save" class="com.xuzhiwen.action.Product" method="save">
                <result name="save">/save.jsp</result>
            </action>
        </package>
    </struts>
    复制代码

    13.新建类:com.xuzhiwen.action.Product ,内容如下:

    复制代码
    package com.xuzhiwen.action;
    
    public class Product {
        private String name;
        private String password;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        @Override
        public String toString() {
            return "Product [name=" + name + ", password=" + password + "]";
        }
        
        public String save(){
            System.out.println("save()..."+this);
            return "save";
        }
        
    }
    复制代码

    14.添加save.jsp ,如下:

    复制代码
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        username:${name}<br>
        password:${password}
    </body>
    </html>
    复制代码

    15. 重启启动tomcat

    16.点击链接:

    17.点击login按钮,结果如下:

  • 相关阅读:
    广佛肇城轨年内通车 佛山西站预计2017年中通车
    MTK+Android编译
    电量检测芯片BQ27510使用心得
    放大电路的分析方法
    放大电路的分析方法
    模拟电子放大电路分析
    模拟电子技术二极管
    unsigned 整型实现无溢出运算
    hdu 5317 RGCDQ(前缀和)
    CodeForces 429 B Working out(递推dp)
  • 原文地址:https://www.cnblogs.com/pegasus827/p/9481912.html
Copyright © 2011-2022 走看看