Struts2无刷新实现登陆退出操作
2015-09-05 16:42
381 查看
运用的jar包:
源代码:
Struts2Test.java
struts.xml源代码:
web.xml源代码:
index.jsp源代码:
success.jsp源代码:
commons-fileupload-1.2.1.jar commons-io-1.3.2.jar commons-logging-1.1.jar freemarker-2.3.13.jar log4j-1.2.17.jar ognl-2.6.11.jar struts2-core-2.1.6.jar struts2-dojo-plugin-2.1.8.1.jar xwork-2.1.2.jar
源代码:
Struts2Test.java
package com.test; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class Struts2Test extends ActionSupport{ 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return "success"; } public String login() { // TODO Auto-generated method stub if(userName.equals("admin")&&password.equals("123456")){ ServletActionContext.getRequest().getSession().setAttribute("userName",userName); ServletActionContext.getRequest().setAttribute("log", "logIn"); }else{ ServletActionContext.getRequest().getSession().removeAttribute("userName"); ServletActionContext.getRequest().setAttribute("log", "failed"); } return SUCCESS; } public String out() { // TODO Auto-generated method stub ServletActionContext.getRequest().getSession().removeAttribute("userName"); ServletActionContext.getRequest().setAttribute("log", "out"); return SUCCESS; } }
struts.xml源代码:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default" namespace="/"> <action name="log" class="com.test.Struts2Test" > <result name="success">/success.jsp</result> </action> </package> </struts>
web.xml源代码:
<?xml version="1.0" encoding="UTF-8"?> <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"> <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>/*</url-pattern> </filter-mapping> </web-app>
index.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <% 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 'index.jsp' starting page</title> <s:head/> <sx:head parseContent="true"/> </head> <body> <s:form action="#" id="formDiv" name="formDiv"> <div id="errorMessage"></div> <s:textfield id="userName" name="userName" label="用户名"></s:textfield> <s:password id="password" name="password" label="密码"></s:password> <!-- s:url标签必须放在sx:submit标签前边,否则无法使用 --> <s:url action="log!login" id="loginAction"></s:url> <!-- %{}是OGNL表达式的写法 --> <sx:submit value="提交" href="%{loginAction}" targets="successDiv" executeScripts="true"></sx:submit> <!-- targets="successDiv":意思是连接到id为successDiv的元素 --> <!-- executeScripts="true":目的是使连接到的页面的js也顺带执行 --> </s:form> <s:div id="successDiv"></s:div> </body> </html>
success.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <%@taglib prefix="sx" uri="/struts-dojo-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <s:head/> <sx:head parseContent="true"/> <base href="<%=basePath%>"> <title>SUCCESS</title> <s:if test="%{#request.log=='logIn'}"> <script type="text/javascript"> document.getElementById("errorMessage").innerHTML=""; document.getElementById("formDiv").style.display="none"; document.getElementById("successDiv").style.display=""; </script> </s:if> <s:if test="%{#request.log=='failed'}"> <script type="text/javascript"> document.getElementById("errorMessage").innerHTML="用户名或密码错误!"; </script> </s:if> <s:elseif test="%{#request.log=='out'}"> <script type="text/javascript"> document.getElementById('formDiv').style.display=''; document.getElementById("successDiv").style.display="none"; </script> </s:elseif> </head> <body> <s:property value="userName"/>,欢迎你! <s:url action="log!out" id="logOut"></s:url> <sx:a href="%{#logOut}" executeScripts="true">退出</sx:a> </body> </html>
相关文章推荐
- 傻瓜式用Eclipse连接MySQL数据库
- Java语言的编译方式
- 将Eclipse中的项目迁移到Android Studio中的方法
- Java开发速度那么慢,这样的竞争对手永远不会让你担心
- java 平衡二叉树的实现
- java入门学习总结——【java菜鸟成长记】
- Maven: Unable to locate the Javac Compiler 解决办法
- java静态方法
- 分布式定时任务框架比较,spring batch, tbschedule jobserver
- quartz集群分布式(并发)部署解决方案-Spring - 推酷
- JavaWeb学习篇之----Session&&Cookie
- java之路——输出HelloWorld
- replace与replaceAll的区别
- java 练习
- Struts2 part 7:文件上传
- java.sql.Blob类型如何插入到MySQL数据库
- 类、对象、继承、封装、多态、方法的重载和重写、Java的访问修饰符与其它关键字
- java语言发展史
- Eclipse快捷键
- Eclipse快捷键