• Struts2入门案例


    1.引入jar包

    2在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>
       <!--  
          constant:常量
          devMoode;开发模式:true: 我在Struts。xml文件中做了修改,tomcat不需要重启
       -->
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
            <action name="loginAction" class="cn.happy.action.LoginAction">
                <result name="success">/login/success.jsp</result>
                 <result name="login">/login/login.jsp</result>
            </action>
        </package>
    
    
    </struts>

    3.建立一个HelloWorldAction类  并实现Action接口,在其中定义一个User 类型的user存储的是实体类中的 username 以及password  并封装

    package cn.happy.action;
    
    import cn.happy.entity.User;
    
    import com.opensymphony.xwork2.Action;
    
    public class HelloWordAction implements Action{
    
        private User user;
        public String execute() throws Exception {
            
            return "success";
        }
        
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
        
    
    }

    4.配置web.xml配置文件并编写filter过滤器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name>    
      
        <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    //可以使用Ctrl+shift+T寻找 </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

    5.index.jsp页面:

    并引用 taglib指令<%@taglib uri="/struts-tags" prefix="s" %>

    注:form表单的action属性名应该和struts.xml的action一致

    <div>
        <h1>
        <s:property value="name"/>
       
        </h1>
        </div>
        
        <div>
        <form action="HelloWordAction" method="post">
        请输入你的姓名:
        <input name="name" type="text"><br/><br/>
        <input type="submit" value="提交">
        
        </form>
        </div>
        
  • 相关阅读:
    Help document of CAD Build
    NSTableView 中 NSTextField无法输入
    NSScrollView 内容显示不正常问题
    Mac 中 NSTrackingArea 鼠标移动事件捕获
    java定时任务调度框架
    uboot: initramfs image to roofs, and rootfs to initramfs image
    jfrog CLI(command line interface)
    git submodule list/clone/update
    python run javascript in ubuntu
    git blame
  • 原文地址:https://www.cnblogs.com/Smile-123/p/5906283.html
Copyright © 2020-2023  润新知