01.创建登录界面

<%@ 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 'index.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>
<form action="user/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
</form> </body>
</html>

02.配置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" namespace="/user" extends="struts-default">
<action name="login" class="cn.bdqn.action.UserAction" method="login">
<result>/success.jsp</result>
</action>
</package>
</struts>

03.创建对应的UserAction

package cn.bdqn.action;

import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户登录的action
* jsp九大内置对象
*
* out
* page
* pageContext
* request
* response
* session
* application
* config
* exception
*/
public class UserAction extends ActionSupport{ /**
public String login(){
获取前台的值
01.耦合方式 不推荐使用
String name = ServletActionContext.getRequest().getParameter("name");
String password = ServletActionContext.getRequest().getParameter("password");
System.out.println(name);
System.out.println(password);
//放入作用域
ServletActionContext.getRequest().setAttribute("name",name);
ServletActionContext.getRequest().setAttribute("password",password);
return SUCCESS;
}*/ /**
* 登录的方法
02. 解耦两种方法
001. 使用ActionContext!
sturts2在底层把我们的request,session,application用Map集合保存起来了!
*/
private String name;
private String password; public String login(){
Map<String, Object> request=(Map<String, Object>) ActionContext.getContext().get("request");
//让success.jsp获取数据
request.put("name",name);// 等同于setAttribute("name",name);
request.put("password",password);
return SUCCESS;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

04.使用第二种解耦的方式 ,就是实现对应的Aware接口

import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 实现对应的Aware接口
*/
public class UserAction2 extends ActionSupport implements RequestAware{ private String name;
private String password;
private Map<String, Object> request; //并不用写 set和get //登录的方法
public String login(){
request.put("name",name);
request.put("password",password);
return SUCCESS;
} //重写方法 给请求request赋值
@Override
public void setRequest(Map<String, Object> request) {
this.request=request; } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

05.success.jsp

  ===============el表达式获取数据=============<br/>
${name}
${password} <br/>
===============struts2标签获取数据=============<br/>
<s:property value="password"/> <%--值栈中获取的 --%>
<s:property value="#request.password"/> <%--栈的上下文中获取的 --%>
<s:property value="#attr.password"/>

最新文章

  1. iOS推送遇到的问题
  2. git bash下对文件的操作
  3. C++ string到底是什么
  4. SAP大数据为&quot;海上F1&quot;提供技术支持
  5. overflow的劲爆知识点
  6. this web application instance has been stopped already解决办法
  7. Chrome浏览器跨域问题
  8. Java NIO内存映射---上G大文件处理(转)
  9. chromium blog
  10. C语言--第四周作业
  11. Android 优质精准的用户行为统计和日志打捞方案
  12. Mac smartsvn破解及license文件
  13. 安装openssh
  14. mysql一列相同另一列相加
  15. BZOJ 5381 or &amp; Codeforces 623E Transforming Sequence DP+NTT
  16. matlab中的reshape快速理解,卷积和乘积之间的转换
  17. MD5小彩虹表
  18. TOML 详解
  19. shell 脚本 实现随机数
  20. linux ubuntu 学习总结(day01)基本命令学习

热门文章

  1. Kettle-1-安装配置
  2. HDU1069:Monkey and Banana(最长上升子序列的应用)
  3. Hadoop MapReduce InputFormat基础
  4. git 上传本地代码到远程仓库
  5. 使用反射实现 webdriver page 类
  6. 在python中打开文件显示没有权限PermissionError: [Errno 13] Permission denied:
  7. Generative model 和Discriminative model
  8. javascript 中的比较大小,兼 typeof()用法
  9. npm国内镜像设置
  10. sqlserver索引的原理及索引建立的注意事项小结