Struts2.1对Ajax的支持登陆实例

1、test.jsp页面:

<%@ page language="java" import="java.util.*;" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>submit和a 标签</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <sx:head/>
</head>
<body>
 <s:url id="login" value="/tologin.action" />
 <sx:a href="%{login}" targets="div1">用户登录</sx:a>
 <sx:div id="div1" cssStyle="boder:1px solid  red">
  第一个DIV,显示登陆菜单。
 </sx:div><br/>
 <sx:div id="div2" cssStyle="boder:1px solid  green">
  第二个DIV,显示登陆菜单。
 </sx:div><br/>
</body>
</html>

2、LoginAction.java

package net.hnspi.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
 private static final long serialVersionUID = 1L;
  private String username;
  private String password;
  
  public String getUsername() {
   return username;
  }

  public void setUsername(String username) {
   this.username = username;
  }

  public String getPassword() {
   return password;
  }

  public void setPassword(String password) {
   this.password = password;
  }

  public String execute() {
   if ("zhanghuxin".equals(username) && "1234".equals(password)) {
    return SUCCESS;
   }

   return ERROR;
  }
}

3、error.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'error.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>
    登录失败!
  </body>
</html>

4、success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'success.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>
    登录成功! <br>
  </body>
</html>

5、struts.xml

<struts>
 <package name="struts2" extends="struts-default">
  <action name="time">
   <result>/time.jsp</result>
  </action>
  <action name="welcome">
   <result>/welcome.jsp</result>
  </action>
  <action name="tologin">
   <result>/login.jsp</result>
  </action>
  <action name="login" class="net.hnspi.action.LoginAction">
   <result name="success">/success.jsp</result>
   <result name="error">/error.jsp</result>
  </action>
 </package>
</struts>

注意:要导入struts2-json-plugin-xxx.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臭丫头站住

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值