用struts2实现简单的登录
来源:互联网 发布:put call parity 知乎 编辑:程序博客网 时间:2024/06/06 16:45
首先先写一个简单的的登录界面,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登录</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="login"> <s:textfield name="username" key="用户名"/> <s:textfield name="password" key="密码"/> <s:submit key="登录dd"/> </s:form> </body></html>
然后,写一个Action,命名为LoginAction,代码如下
package com.dhy.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction 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 { System.out.println(getUsername()+getPassword()); // TODO Auto-generated method stub ActionContext ctx=ActionContext.getContext(); //通过ActionContext访问application范围的属性值 Integer counter=(Integer)ctx.getApplication().get("counter"); if(counter==null){ counter=1; }else{ counter=counter+1; } ctx.getApplication().put("counter", counter); //通过ActionContext设置session范围的属性 ctx.getSession().put("user", username); if(getUsername().equals("jocean")&&getPassword().equals("jocean")){ //通过ActionContext设置request范围的属性 System.out.println("in"); ctx.put("tip","服务器提示:您已经登录成功"); return SUCCESS; } //通过ActionContext设置request范围的属性 ctx.put("tip", "服务器提示:登录失败!"); return ERROR; }}
配置struts2.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> <!-- 指定全局国际化资源文件 --> <!-- add --> <!-- <constant name="struts.custom.i18n.resources" value="mess"/> --> <!-- End --> <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> --> <package name="crazyit" namespace="/" extends="struts-default"> <action name="login" class="com.dhy.action.LoginAction"> <result name="error">/WEB-INF/content/error.jsp</result> <result name="success">/WEB-INF/content/success.jsp</result> </action> </package> <!-- <include file="example.xml"/> --> <!-- Add packages here --></struts>
然后,写success.jsp和error.jsp,其中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>成功页面</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> 访问次数:${applicationScope.counter}<br/> ${sessionScope.user},您已经登录!<br/> ${requestScope.tip} </body></html>
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> ${requestScope.tip} </body></html>
web.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- struts2配置 ,添加过滤器,如果是通过工具添加,会自动生成--> <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> <!-- End --></web-app>
目录结构如下图所示:
运行结果如下:
0 0
- 用struts2实现简单的登录
- Struts2实现简单的登录效果
- struts2 简单实现用户登录
- struts2实现登录用户的简单的权限设置
- Spring+Struts2+JQuery配合实现简单的登录功能
- struts2的环境配置与简单登录功能实现
- struts2入门学习篇:实现一个简单的登录操作
- eclipse下用J2EE(hibernate,struts2,spring)实现的简单登录、注册系统
- Hibernate+Struts2实现简单登录注册
- Hibernate+Struts2实现简单登录注册
- 使用Struts2实现简单用户登录
- 实现登录的Struts2应用程序
- 简单的登录实现
- 一个简单登录的Struts2的示例
- 复习Struts2之简单的登录示例
- struts2学习-简单的登录功能
- 简单实例: 基于Struts2的登录系统
- Struts2登录拦截器的简单示例
- 占位
- 占位
- JAVA学习20_Java中String与byte[]的转换
- MySql 模糊查询特殊字符处理
- 杨令云玩过的那些FC游戏-魔道士的阴谋(二)
- 用struts2实现简单的登录
- 占位
- 占位
- 占位
- 占位
- 占位
- 占位
- 占位
- scala进阶23-for与模式匹配