`

struts02:占位符、通配符

 
阅读更多

第一:概念:无

 

第二:struts2通配符的使用(!是在jsp页面的时候用的)

第一步:struts.xml中的action标签不写method属性了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <package name="my" namespace="/" extends="struts-default">
     <action name="login" class="com.action.TestAction" >
         <result name="success">success.jsp</result>
         <result name="error">error.jsp</result>
     </action>
   </package>
</struts>   

第二步: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>
   <form action="login!UserLogin.action">
                         姓名: <input type="text" name="test.name"><br>
                        年龄:   <input type="text" name="test.age"><br>  
        <input type="submit" value="提交">   
   </form>
  </body>
</html>

第三步:action层:必须有名字为UserLogin的方法

package com.action;

import com.entity.Test;

public class TestAction {
 private Test test;

 public Test getTest() {
  return test;
 }

 public void setTest(Test test) {
  this.test = test;
 }
 public String UserLogin(){
  if(test.getName().equals("admin")&&test.getAge()==8){
   return "success";
  }
     return "error";
 }

}

 

 

第三:使用占位符和通配符

1.struts.xml的配置(其中*表示通配符,{1}表示占位符)

注意:*所表示的内容就是{1}所要表示的内容

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <package name="my" namespace="/" extends="struts-default">
     <action name="login_*" class="com.action.TestAction" method="{1}">
         <result name="success">success.jsp</result>
         <result name="error">error.jsp</result>
     </action>
   </package>
</struts>   

2.jsp页面(在表单提交的时候,占位符的位置使用UserLogin字符串替代了,体现了通配符)

<%@ 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="login_UserLogin.action">
                         姓名: <input type="text" name="test.name"><br>
                        年龄:   <input type="text" name="test.age"><br>  
        <input type="submit" value="提交">   
   </form>
  </body>
</html>

3.action类(由于jsp页面在表单提交的时候,使用了login_UserLogin.action,因此在action类中必定会出现UserLogin方法,因为struts.xml中的占位符{1}就是给struts.xml中的通配符*用的)

package com.action;

import com.entity.Test;

public class TestAction {
 private Test test;

 public Test getTest() {
  return test;
 }

 public void setTest(Test test) {
  this.test = test;
 }
 public String UserLogin(){
  if(test.getName().equals("admin")&&test.getAge()==8){
   return "success";
  }
     return "error";
 }

}

第四:课外知识

最为简单的action配置可以说是一个action对应一个类。形如下面:
<package name="schoolweibo" extends="struts-default">

<action name="register" class="registerAction">
<result name="success">/page/user_Login.jsp</result>
</action>
</package>
这样虽然清晰明了,但是如果有太多的Action,那就要写很多配置文件,基于此,struts2提供了模糊匹配;也就是动态定位。
<package name="schoolweibo" extends="struts-default">

<action name="user_*" class="indexAction" method="{1}">
<result name="success">/page/user_{1}.jsp</result>
</action>
</package>
其中【*】号代表占位符,而【{1}】表示【*】所对于的action中的方法,比如jsp页面中这样写:
<a href="user_register.action">立即注册</a>
那么对于的indexAction类中的就是register方法。注意:这样写的前提是必须把execute方法去掉,即不要重写。这还不是最少配置的。
下面来看一个最少配置的。
<package name="schoolweibo" extends="struts-default">

<action name="*_*" class="{1}Action" method="{2}">
<result name="success">/page/{1}_{2}.jsp</result>
</action>
</package>
这里有两个占位符,第一个代表对于的action类名,第二个代表方法名。如果还有更多占位符,则可以以此类推。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics