Struts2 配置通配符和错误页面

图示:

 

配置 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">
	<display-name></display-name>
	<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>

 

User 实体类

package entity;

public class User {
	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 say(){
		return "这是一个方法";
	}
}

 

DefaultAction Action类

package action;

import com.opensymphony.xwork2.Action;

public class DefaultAction implements Action {

	public String execute() throws Exception {
		return ERROR;
	}

}

 

LoginAction Action 类

package action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import entity.User;

public class LoginAction implements Action {
	private User user;
	private String message;
	private String role;

	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String execute() throws Exception {
		//判断账号密码是否正确
		if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){
			//session
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("USER1", user);
			//request
			Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
			request.put("USER2", user);
			//application
			Map<String, Object> application = ActionContext.getContext().getApplication();
			application.put("USER3", user.getUsername());
			//返回成功
			return SUCCESS;
			//用来测试返回 ERROR 的是否全局了
		}else if("".equals(user.getUsername())&&"".equals(user.getPassword())){
			return ERROR;
		}else{
			this.message = "用户名或者密码错误";
			//返回重新输入
			return INPUT;
		}
	}

	public String dynamic() throws Exception{
		if("admin".equals(user.getUsername())){
			this.role="ADMIN";
		}else{
			this.role="USER";
		}
		return SUCCESS;
	}	

	public String adminLogin() throws Exception{
		return SUCCESS;
	}
	public String genricLogin() throws Exception{
		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"></constant>

	<!-- 配置文件修改的时候被重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>

	<!-- 修改系统的主题(页面) -->
	<!-- constant name="struts.ui.theme" value="simple"></constant -->

	<!--启动动态方法调用 -->
	<!--  constant name="struts.enable.DynamicMethodInvocation" value="true"></constant -->

	<package name="default" namespace="/" extends="struts-default">
		<!-- 非存在 action 的所有 -->
		<default-action-ref name="defaultAction"></default-action-ref>


		<!-- 全局结果集,如果返回的是 error 集中显示此页面 -->
		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>


		<action name="login" class="action.LoginAction">
			<result name="success">welcome.jsp</result>
			<result name="input">login.jsp</result>
		</action>
		<!-- 使用通配符,完成两个角色的登陆 -->
		<action name="*User" class="action.LoginAction" method="{1}Login">
			<result name="success">{1}Page.jsp</result>
		</action>

		<!-- 非存在 action 的所有,都执行该 action -->
		<action name="defaultAction" class="action.DefaultAction"></action>
	</package>
</struts>

 

adminPage.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 '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>
	<h4>管理员登陆</h4>
	<s:form action="login">
		<s:textfield label="管理员" name="user.username"></s:textfield>
		<s:password label="密码" name="user.password"></s:password>
		<s:submit value="提交"></s:submit>
	</s:form>
	<h4>
		<s:property value="message" />
	</h4>
</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 '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>
    这是错误页面。
  </body>
</html>

 

genricPage.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 '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>
	<h4>普通用户登陆</h4>
	<s:form action="login">
		<s:textfield label="用户名" name="user.username"></s:textfield>
		<s:password label="密码" name="user.password"></s:password>
		<s:submit value="提交"></s:submit>
	</s:form>
	<h4>
		<s:property value="message" />
	</h4>
</body>
</html>

 

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>这是登陆页面</title>
</head>

<body>
	<form action="login.action">
		用户名:<input type="text" name="user.username" /><br /> 密码:<input
			type="text" name="user.password" /><br /> <input type="submit"
			value="提交">
	</form>
	<h4>${message }</h4>
	<hr />
	<s:form action="login">
		<s:textfield label="用户名" name="user.username"></s:textfield>
		<s:password label="密码" name="user.password"></s:password>
		<s:submit value="提交"></s:submit>
	</s:form>
	<h4>
		<s:property value="message" />
	</h4>
</body>
</html>

 

welcome.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>这是欢迎页面</title>
</head>

<body>
	<s:debug></s:debug>
	<h4>This is welcome Page</h4>
	<h4>传统方式拿出来的值</h4>
	<h4>${sessionScope.USER1.username }(从 session 拿出来)</h4>
	<h4>${requestScope.USER2 }(从 request 拿出来)</h4>
	<h4>${applicationScope.USER3 }(从 application 拿出来)</h4>
	<hr />
	<h4>Struts2 Tag</h4>
	<h4>
		<s:property value="#session.USER1.username" />
		(从 session 拿出来,PS可以通过对象拿属性)
	</h4>
	<h4>
		<s:property value="#request.USER2.say()" />
		(从 request 拿出来,PS可以通过对象拿方法)
	</h4>
	<h4>
		<s:property value="#application.USER3" />
		(从 application 拿出来,PS可以直接取到对象)
	</h4>
</body>
</html>

 

效果图:



 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值