Struts2 学习笔记之---使用通配符配置默认Action(验证框架为例说明)

使用通配符(*)配置默认Action的另一种方法。在配置的最后使用通配法Action能够捕捉没有匹配的action请求。配置如下:

<action name="*">
    <result>/{1}.jsp</result>
</action>

由于“*”可以匹配所有的action,因此,通配符默认Action配置必须放置在配置文件的最后。
在上表面的代码段中,在result的配置中还有一个占位符{1},它将被通配符的结果所替换,即如果“*”匹配了hello,则{1}用hello来替换。

以struts的验证框架为例说明如下:
1)视图文件
1、index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <title>
        Employee From
    </title>
    </head>
    <body>
        <s:form action="login" method="post">
            <s:textfield name = "name" label ="Name" size="20"/>
            <s:textfield name="age" label="Age" size="20"/>
            <s:submit name="submit" label="Submit" align="center"/>
        </s:form>
    </body>
</html>

2、login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Success</title>
    </head>
    <body>
    <h1>Login</h1>
    Employee information is captured successfully!!!
    </body>
</html>

2)Action类
Employee.java

package test.verify.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Employee extends ActionSupport {
    private String name;
    private int age;

    public String execute() {
        return SUCCESS;
    }
    public String getName() {
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void validate() {
        if (name == null || name.trim().equals("")) {
            addFieldError("name", "The name is required");
        }
        if (age < 28 || age > 65) {
            addFieldError("age", "Age must be in between 28 and 65");
        }
    }
}

3)配置文件
1、struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
   <struts>
        <constant name ="struts.devMode" value="true"/>
        <package name ="helloworld" extends="struts-default">
            <action name="*"
                class="test.verify.struts2.Employee"
                method="execute">
                <result name="input">/index.jsp</result>
                <result name="success">/{1}.jsp</result>
            </action>
        </package>
   </struts>

该配置文件下name="*",<result name="success">/{1}.jsp</result>
2、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">

   <display-name>Struts 2</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.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

4)功能测试
两个文本框,分别输入:
case1: Name为空, Age 为 3 。文本框上出现提示文字
这里写图片描述
case2:Name为struts,Age为30
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值