Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565618
  • 博文数量: 136
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 1343
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 23:18
文章分类

全部博文(136)

文章存档

2011年(28)

2009年(60)

2008年(48)

我的朋友

分类: Java

2011-05-13 23:34:32

今天试着跑个例子,发现出了问题。
Login.JSP
 

<script type="text/javascript">
        function regist()
        {
            var tgForm = document.forms[0];
            tgForm.action = "LogReg!regist.action";
            tgForm.submit();
        }
    </script>
  </head>
  
  <body>
    <form action="LogReg!login.action" method="post" name="loginForm">
        <table align="center">
            <caption>用户登录</caption>
            <tr>
                <td>用户名:<input type="text" name="username"/></td>
            </tr>
            <tr>
                <td>&nbsp;&nbsp;码:<input type="text" name="password" /></td>
            </tr>
            <tr align="center">
                <td colspan="2"><input type="submit" value="登录"/><input type="button" value="注册" onclick="regist()" /> </td>
            </tr>
        </table>
    </form>
  </body>
</html>

 

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1/EN"
    "">
    <struts>
        <package name="struts2" extends="struts-default">
            <action name="LogReg" class="com.augur.actions.LoginAction">
                <result name="input" >/login.jsp</result>
                <result name="success" type="redirect">/welcome.jsp</result>
                <result name="error" type="redirect">/error.jsp</result>
            </action>
        </package>
</struts>

 

LoginAciton.java

package com.augur.actions;


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


@SuppressWarnings("serial")
public class LoginAction extends ActionSupport
{
    private String username;
    private String password;
    private String tip;


    //get,set方法略
    public String login() throws Exception
    {
        ActionContext ctx = ActionContext.getContext();
        Integer counter = (Integer)ctx.getApplication().get("counter");
        if(counter == null)
        {
            counter = 1;
        }
        else            
        {
            counter++;
        }
        ctx.getApplication().put("counter", counter);
        ctx.getSession().put("user", getUsername());
        
        if("xpj".equals(getUsername()) && "xpj".equals(getPassword()))
        {
            System.out.println("验证通过。。。");
            ctx.put("tip", "服务器提示:您已成功登陆!");
            return ActionSupport.SUCCESS;
        }
        else
        {
            System.out.println("验证失败....");
            ctx.put("tip","服务器提示:登陆失败!");
            return ActionSupport.ERROR;
        }
    }
    
    
    public String regist() throws Exception
    {
        ActionContext ac = ActionContext.getContext();
        ac.put("tip","您已经成功登陆注册界面2");
        return SUCCESS;
    }
}


welcome.jsp

    访问次数:${applicationScope.counter}<br>
    ${sessionScope.user},您已成功登陆!<br>
    Request信息: ${requestScope.tip}

发现在LoginAction里面设置的tip在welcome.jsp页面上获取到的值为空,看了代码也没发现什么问题。最后仔细看了配置文件,原来result 标签中我定义了type="redirect",设置了重定向,才会使设置tip的值被置空。只要把不设置type属性即可,也就是采用默认值即可。默认的是dispatcher;

阅读(13488) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~