Action的概念

一.如何开发Action

理论上Action组件不需要继承任何类,实现任何接口,他可以只是一个简单的Java对象。但Strut2还是提供了一些辅助工具。

1.      框架首先提供了一个可选的Action接口。

Pubic interface Action{

           Publicstatic final String ERROR=”error”;

           Publicstatic final String INPUT=”input”;

           Publicstatic final String LOGIN=”login”;

           Publicstatic final String NONE=”none”;

           Publicstatic final String SUCCESS=”success”;

           Publicstring execute() throws Exception;

}

2.      另外,框架还提供了一个Action的组件的基类ActionSupport,在它实现了Action接口的同时,还实现了另外四个接口:Validateable和ValidationAware接口提供验证功能;TextProvider和LocaleProvide接口提供对本地化和国际化的支持。

二.Action做了什么事

Action对web请求的处理包含以下步骤

1.      获取Web请求中的信息

2.      在execute方法中根据获取的Web请求信息进行业务逻辑处理。

3.      返回一个代表处理结果的字符串。

三.项目ActionDemo01,模拟登陆程序

Web.xml

<?xml version="1.0" encoding="GBK"?>

<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>

    <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>

 

 

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%@taglib prefix="s" uri="/struts-tags" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>登陆</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">

 

  </head>

 

  <body>

  <h4>请输入你的用户名和密码:</h4>

  <s:form action="Login">

        <s:textfield name="username" label="用户名"/>

        <s:password name="password" label="密码" />

        <s:submit label="登陆" />

  </s:form>

  </body>

</html>

 

 

Success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%@taglib prefix="s" uri="/struts-tags" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>登陆成功</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">

 

  </head>

 

  <body>

  <h4><s:property value="username"/>登陆成功</h4>

  </body>

</html>

 

 

Action:

Login.java

package com.luocky.struts2Model;

import com.opensymphony.xwork2.ActionSupport;

 

public class Login extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private String username;

    private String password;

   

    @Override

    public String execute(){

       if(username.equals(password)){

           return SUCCESS;

       }else{

           return INPUT;

       }

    }

    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;

    }

}

 

Struts.xm;

<?xml version="1.0" encoding="GBK" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

     <package name="default" namespace="/" extends="struts-default">

       <action name="Login" class="com.luocky.struts2Model.Login">

           <result name="success">/Success.jsp </result>

           <result name="input">/Login.jsp</result>

       </action> 

    </package>

</struts>

 

 

执行过程中出现的问题:在Struts.xml中时,误写成

<result name="SUCCESS">/Success.jsp </result>

           <result name="INPUT">/Login.jsp</result>

时,出现找不到Action的错误

 


你可能感兴趣的:(html,String,struts,action,login,encoding)