Struts2(四)命名空间的查询顺序以及默认执行的Action

01.创建login.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> <%-- 1.验证命名空间的查询顺序 : 从后往前,依次去掉一级目录(/)! user/user2/user1/login 01.最后一个/后面的是 action的name!这是固定的 02.它会把login之前的所有路径当成(user/user2/user1)namespace去查询 03.会把user1之前的所有路径当成(user/user2)namespace去查询 04.会把user2之前的所有路径当成(user/)namespace去查询 --%> <a href="user/login">登录1</a> <a href="user/user2/user1/login">登录2</a> <a href="user1/user2/user/login">登录3</a> </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"> <!--method:默认execute() 如果指定了具体的方法 就去执行指定的! --> <!-- <action name="login"> 省略了class属性,默认去执行ActionSupport中的execute(); 因为ActionSupport中的execute()返回success,所以我们能得到正确的页面显示! 怎么知道默认去执行ActionSupport中的execute()? 因为我们继承了struts-default这个包 最下面有一句 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /> action默认执行的类! --> <default-class-ref class="cn.bdqn.action.LoginAction"/> <!-- 如果一个类中有对应的多个action,那么我们只需要配置一个全局默认的default-class-ref即可! --> <action name="login" method="login"> <result name="success">/loginSuccess.jsp</result> </action> </package> </struts>
复制代码

对应的struts-default.xml文件中的默认配置

03.创建对应的Action

复制代码
package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * 用户登录的action */ public class LoginAction extends ActionSupport{ @Override public String execute(){ System.out.println("默认执行execute()"); return "success"; } //用户登录的方法 public String login(){ System.out.println("执行login()"); return "success"; } }
复制代码

04.创建success.jsp

复制代码
<%@ 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>My JSP 'success.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> <h1>登录成功页面</h1> </body> </html>
复制代码

猜你喜欢

转载自www.cnblogs.com/xiaobaizhang/p/8965836.html