• struts2 Demo


    参考资料 :
    http://www.cnblogs.com/yangy608/archive/2010/11/08/1871962.html
    http://www.yiibai.com/struts2/struts_quick_guide.html

    个人理解:
    首先我们要清楚struts是一种MVC框架、
    strut1和struts2有所区别。最明显的应该是Struts2把Struts1的actionForm和action的结合使用,并且优化了execute方法,通过String就可以实现跳转文件。

    struts.xml
    该配置文件的作用:配置action。
        action配置:
            name   action名    
            class  类路径
            result:action result 页面跳转配置
            
    (个人理解:这里比struts1有所简化,省去了form的配置以及action和form 的关联配置.)

    以下为struts2的Demo工程源码:

    1.建立web工程,引入struts2包

    2.建立package:action包

      2.1在action下建立LoginAction.java

    package action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        /*
         * (non-Javadoc)
         * 
         * @see com.opensymphony.xwork2.ActionSupport#execute()
         */
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            System.out.println("LoginAction.execute()");
            System.out.println("name = " + name);
            if (name.equals("xiaoming")) {
                return "success";
            } else {
                return "error";
            }
        }
    
        private String name;
    
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
    
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    
    }

    3.配置文件

      3.1在scr文件夹下建立struts.xml

    (这里的package 的extends一直显示undefined,故引入struts-default.xml文件,result文件也要记得配置好)

    <之前一直报错:警告: Could not find action or result
    No result defined for action action.LoginAction and result error

    解决办法是在package加上 namespace="/"

    >

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
     "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
        <include file="struts-default.xml"></include>
        <package name="struts2" namespace="/" extends="struts-default">
            <action name="login" class="action.LoginAction">
                <result name="success">/WEB-INF/wel.jsp</result>
                <result name="error">/WEB-INF/error.jsp</result></action>
        </package>
    </struts>    

      3.2web.xml:

    (注意 filter-class文件,应该是说struts2.1版本以上的使用如下文件,struts2.0的是另一个文件)

    <?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>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <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>*.action</url-pattern>
      </filter-mapping></web-app>

    4.编写jsp页面:

      4.1 编写input-source 页面index.jsp

    <form action="login.action">
            name:<input type="text" name="name"> <input type="submit"
                value="submit">
        </form>

      4.2 编写forward页面 error.jsp 和 wel.jsp

    <body>
        This is wel JSP page. <br>
        hello <%=request.getParameter("name") %>
      </body>
    --------------------
    做一个精神上的素食主义者。
  • 相关阅读:
    sql server 分页
    省市区
    省市
    sql server 中英混合排序
    C# 添加大量sql
    小程序小数的输入判定
    C# Files 的值“<<<<<<< .mine”无效。路径中具有非法字符。
    vagrant安装遇到的问题
    vagrant安装使用
    tp6 session问题
  • 原文地址:https://www.cnblogs.com/xfile/p/4092621.html
Copyright © 2020-2023  润新知