Struts2.0登陆小例子

这几天没事,看这论坛里都在谈struts2,所以也起了好奇心,自己做了一个简单的登陆,实现了从login.jsp,提交数据给服务器,服务器判断提交的数据是否为用户名:"zhangsan",密码:"lisi",如果是就显示"zhangsan ,欢迎登陆.,还有就是支持中文显示,给大家一个参考...接触时间不长,请大家多提意见...

  1. 先去官方网站上下载struts2的jar包,下载地址是: http://apache.mirror.phpchina.com/struts/binaries/struts-2.0.11.1-all.zip(其中包含原代码,文档和例子,一个比较全的).
  2. 说明一下,我的环境是xp的系统,myeclipse5.1的.然后就创建一个web工程.在web-inf目录下的lib目录中添加jar包,commons-logging-1.0.4,freemarker-2.3.8,ognl-2.6.11,struts2-core-2.0.11.1,xwork-2.0.4等,这些包是什么作用,我也不太清楚,我刚接触太不到一天,这个web.xml文件中配置的好象是一个过滤器.然后把web.xml文件内容配置成如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    >

        
    <!--     <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
            http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    -->

        
    <filter>
            
    <filter-name>struts2</filter-name>
            
    <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            
    </filter-class>
            
    <!--        <init-param></init-param>-->
        
    </filter>
        
    <filter-mapping>
            
    <filter-name>struts2</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>

        
    <welcome-file-list>
            
    <welcome-file>/login.jsp</welcome-file>
        
    </welcome-file-list>
    </web-app>
  3. 然后创建action类,在struts2中不需要actionform类,并且struts2中的action也不需要继承一个特定的action类,但一般情况他都会继承这个类com.opensymphony.xwork2.ActionSupport,然后重写他的execute()方法,该方法返回一个字符串,学过jsf的朋友应该知道的,这和jsf有点相似,刚才说他不需要actionform,其实现在的action中定义属性即可,然后给他们getter和setter方法,LoginAction代码如下:
    package  com.struts2;

    import
     com.opensymphony.xwork2.ActionContext;
    import
     com.opensymphony.xwork2.ActionSupport;

    public class LoginAction extends ActionSupport 
    {

        
    /**
         * 
         
    */

        
    private static final long serialVersionUID = 1L;

        
    private
     String userName;

        
    private
     String userPass;

        
    public String getUserName() 
    {
            
    return
     userName;
        }


        
    public void setUserName(String userName) {
            
    // org.apache.struts2.dispatcher.FilterDispatcher

            this.userName = userName;
        }


        
    public String getUserPass() {
            
    return
     userPass;
        }


        
    public void setUserPass(String userPass) {
            
    this.userPass =
     userPass;
        }


        @Override
        
    public String execute() throws Exception {
            
    // TODO Auto-generated method stub

            if ("zhangsan".equals(userName) && "lisi".equals(userPass)) {
                ActionContext.getContext().getSession().put(
    "user"
    , getUserName());
                
    return
     SUCCESS;
            }
     else {
                
    return
     INPUT;
            }

        }

    }

  4. 接着就来配置这个action,这个文件的名字叫做struts.xml有点象struts1中的struts-config.xml文件.他的存放路径我现在只知道可以放在classes下,也就是src目录下,action中配置了action路径,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>
        
    <!--    <constant name="struts.enable.DynamicMethodInvocation"-->
        
    <!--        value="false" />-->
        
    <!--    <constant name="struts.devMode" value="false" />-->
        
    <package name="struts2" extends="struts-default">
            
    <action name="loginAction" class="com.struts2.LoginAction">
                
    <result name="input">/login.jsp</result>
                
    <result name="success">/suc.jsp</result>
            
    </action>
            
    <!--        <action name="....." class="....." method=".....">-->
            
    <!-- 其中name可以随便起,这是以后自己要用到的,class就是类的全名,刚才说action不需要继承某个类,
                也就是说他可以不遵循某中规范,可以在自定义的action中自定义处理方法
    -->

            
    <!--            <result>........</result> 相当与actionforward-->
            
    <!--        </action>-->
        
    </package>
    </struts>
  5. 编写jsp文件,很简单如代码:
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        
    <head>

            
    <title>My JSP 'login.jsp' starting page</title>

            
    <meta http-equiv="pragma" content="no-cache">
            
    <meta http-equiv="cache-control" content="no-cache">
            
    <meta http-equiv="expires" content="0">
            
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            
    <meta http-equiv="description" content="This is my page">
            
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
    -->

        
    </head>

        
    <body>
            
    <s:form action="loginAction">
                
    <%--        <form action="LoginAction.action" method="post">--%>
                
    <s:textfield name="userName" label="用户名"></s:textfield>
                
    <br>
                
    <s:password name="userPass" label="密  码"></s:password>
                
    <br>
                
    <s:submit></s:submit>
                
    <%--        </form>--%>
            
    </s:form>
            
    <br>
        
    </body>
    </html>
    suc.jsp文件如下:
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <jsp:directive.page import="com.struts2.LoginAction" />
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        
    <head>
            
    <title>My JSP 'suc.jsp' starting page</title>

            
    <meta http-equiv="pragma" content="no-cache">
            
    <meta http-equiv="cache-control" content="no-cache">
            
    <meta http-equiv="expires" content="0">
            
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            
    <meta http-equiv="description" content="This is my page">
            
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
    -->

        
    </head>

        
    <body>
            ${sessionScope.user },欢迎登陆。。。。。
            
    <br>
        
    </body>
    </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值