struts2 命名空间,基本流程配置

本实例实现基本的登陆功能:
1,配置xml文件,让struts2拦截所有请求:
<?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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- STRUTS2的核心filter拦截所有的请求,
当用户的请求过来,解析、封装参数,解析配置文件 struts.xml然后通过反射来创建Action实例,并调用Action
中指定的方法.
在struts2中filter是核心控制器,
Acton是业务控制器。所有Struts是有两个控制器的。
-->
<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>


2,编写登陆处理逻辑的Action
package com.supan.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.config.entities.ActionConfig;
/*
* 为了让用户更加规范的开发Action,struts2已经定义了一个名为Action的接口,
* struts也建立了一个类实现了Action接口这个类就是ActionSupport类,尽管我们
* 的Action可以不实现任何接口也不继承任何类,但是为了开发的统一性、完整性、
* 我们还是最好继承这个ActionSupport类
* ActionSupport已经实现Action接口和Validateable接口
*/
public class LoginAction extends ActionSupport {
/*
* struts2 的Action中有两种属性:一种是页面传进来的属性,另一种是处理结果的属性
* struts2不会去区分这两种属性的,也就是说这两种属性的地位是完全平等的。
*/
//action中接受页面传过来的属性
private String username;
private String password;
//action中返回给页面的处理结果
private String tip;
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
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());
System.out.println(getPassword());
ActionContext acx = ActionContext.getContext(); //获取Action的上下文对象
if(getUsername().equals("chenchaoyang") && getPassword().equals("supanccy")){
//在action用ActionContext来访问ServletApI
Integer counter = (Integer)acx.getApplication().get("counter");
if(counter == null){
acx.getApplication().put("counter", 1);
}else{
acx.getApplication().put("counter", counter+1);
}
acx.getSession().put("user", getUsername());

acx.put("tip", "服务器提示:登陆成功");
setTip("服务器提示:登陆成功!");
return SUCCESS;
}else{
acx.put("tip", "服务器提示:登陆成功");
setTip("服务器提示:登陆失败!");
return INPUT;
}
}
}

3,登陆界面login.jsp;
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-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 'login.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>
This is my JSP page. <br>
<s:form action="/login/login.action">
<s:textfield name="username" label="用户名"></s:textfield>
<s:textfield name="password" label="密码"></s:textfield>
<s:submit value="登陆"></s:submit>
<s:property value="tip"/>
<s:property value="username"/>
<s:property value="password"/>
</s:form>
</body>
</html>

4,登陆成功界面loginsuccess.jsp;
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-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 'loginsuccess.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>

<s:property value="tip"/><br>
<s:property value="username"/><br>
<s:property value="password"/><br>
__________________________________________________________<br>

本站总访问量: ${applicationScope.counter }<br>
当前登陆用: ${sessionScope.user }<br>
服务器提示: ${requestScope.tip }<br>
</body>
</html>

5,登陆失败界面loginerror.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-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 'loginsuccess.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>

<s:property value="tip"/><br>
<s:property value="username"/><br>
<s:property value="password"/><br>
__________________________________________________________<br>

本站总访问量: ${applicationScope.counter }<br>
当前登陆用: ${sessionScope.user }<br>
服务器提示: ${requestScope.tip }<br>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值