zoukankan      html  css  js  c++  java
  • Struts2入门案例

    struts2最简便的案例

     

    Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点.Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架下开发的程序提供很好的兼容性。下面我们了解一下syruts2的应用

    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>
        
    复制代码
  • 相关阅读:
    1069. Prufer Code 夜
    CROCMBTU 2012, Elimination Round (ACMICPC) D. Restoring Table 夜
    CROCMBTU 2012, Elimination Round (ACMICPC) H. Queries for Number of Palindromes 夜
    1145. Rope in the Labyrinth 夜
    1721. Two Sides of the Same Coin 夜
    1182. Team Them Up! 夜
    1162. Currency Exchange 夜
    1056. Computer Net 夜
    FOJ 2013 A short problem
    Codeforces 11.23
  • 原文地址:https://www.cnblogs.com/yangronglin/p/5906373.html
Copyright © 2011-2022 走看看