zoukankan      html  css  js  c++  java
  • Eclipse搭建Struts框架,及一个简单的Struts例子

     一.下载struts2.0.1
    http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包含了开发struts2所需的struts2-core.jar核心包以及其它struts2所依赖的JAR文件,另外还有一些struts2的示例程序以及一些HTML的API文档。
    
    二.试用struts2.0.1
     
    1. 新建一个WEB工程,将struts-2.0.1-all.zip压缩包中的lib目录下的所有jar文件拷贝到WEB工程的/WEB-INF/lib目录下。
    
    修改WEB-INF下的web.xml文件,加入如下内容:
    
    <filter>
            <filter-name>struts2</filter-name>
            <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            </filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- 这里是设置struts2标签,也可以不用设置,因为在struts-core.jar的META-INF目录下已经包含了
            这个tld文件,J2EE容器会自动地加载它 -->
        <jsp-config>
            <taglib>
                <taglib-uri>/s</taglib-uri>
                <taglib-location>
                    /WEB-INF/tlds/struts-tags.tld
                </taglib-location>
            </taglib>
        </jsp-config>
    在web.xml中定义了一个struts2的FilterDispathcer的filter,这个FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 
    
           2. 新建一个登录页面login.jsp:
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
    <title>登录页面</title>
    </head>
    <body>
    <s:form action="login">
        <table align="center">
        <caption><h3>用户登录</h3></caption>
            <tr>
                <td><s:textfield label="用户名" name="username" /></td>
            </tr>
            <tr>
                <td><s:password label="密  码" name="password" /></td>
            </tr>
            <tr align="center">
                <td><input type="submit" value="登录"/></td><td><input type="reset" value="重填" /></td>
            </tr>
        </table>
    </s:form>
    </body>
    </html>
    
    3.编写Action login:
    
    package org.rainlife.struts2.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
        private String username;
    
        private String password;
    
        @Override
        public String execute() throws Exception {
            if (!(getUsername().equals("rainlife"))
                    && !(getPassword().equals("rainlife"))) {
                return ERROR;
            } else {
                return SUCCESS;
            }
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
    }
    
    在这个LoginAction类中,继承了ActionSupport。ActionSupport 是xwork2这个开源框架中的一个让action能够更加快速地工作的基类,它包含了action中许多可选服务的默认实现,可以让我们更加容易地自定义一个action。
    
    在这里我们定义了username和password两个属性并提供了相应的get/set方法。并且定义了一个execute()方法,该方法覆盖ActionSupport类中的execute()方法,可以看到,它只是简单地返回一个字符串(”SUCCESS”或”INPUT”,而不像是在struts1中的返回一个ActionForward,这两个字符串也是在ActionSupport中定义的,在ActionSupport中定义了四个String属性,分别为SUCCESS,INPUT,ERROR,LOGIN。
    
    这样,我们的action就已经完成了,但还存在一个问题,怎么样让struts2知道我们这个自定义的action,并且可以在HTML/JSP页面中将动作提交给action呢?答案是需要配置struts.xml文件。
    
           4.配置struts.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="struts2" extends="struts-default">
            <action name="login" class="org.rainlife.struts2.action.LoginAction">
                <result name="error">/error.jsp</result>
                <result name="success">/success.jsp</result>        
            </action>
        </package>
    </struts>
    
    在这个struts.xml配置文件中,可以发现和以前的struts-config.xml已经完全不一样了,而在webwork的配置文件非常相似。在这里,我们定义一个名name=”login”的action,通过class属性指向了刚才我们创建的LoginAction类,这样,就将我们定义的action告诉给了struts2。而在HTML/JSP页面中,可以通过这个”login”这个name来将动作提交给相应的Action。
    
    如果在package中设置了namespace属性,如namespace=”/struts2”,则在JSP页面中应该将Form的action设置为”/struts2/login.action”。
    
           5.创建error.jsp和success.jsp:
    
    error.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
        <head>
            <title>错误页面</title>
        </head>
        <body>
            您不能登录!
        </body>
    </html>
    
    success.jsp
    
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
        <head>
            <title>成功页面</title>
        </head>
        <body>
            您已经登录!
        </body>
    </html>
    
    Struts2会根据在LoginAction中返回的字符串(ERROR或SUCCESS)来和在struts.xml中<result>中的name属性所定义的字符串匹配,并跳转到相应页面。
  • 相关阅读:
    Json
    JQuery的validate不起作用的情况
    ajax的同步异步
    Bootstrap--switch
    Bootstrap--multiselect
    ArcGIS地图打印那些事
    openlayers调用瓦片地图分析
    多种在线地图综合对比,Google,必应,arcgis Online...
    map的infowindow的show事件(ArcGIS API for JS)
    在ArcGIS中导出现有mxd的style文件
  • 原文地址:https://www.cnblogs.com/timssd/p/4285205.html
Copyright © 2011-2022 走看看