Struts2系列(二)Action

一.Action是Struts2的核心,所有用户请求都需要使用Action处理并返回数据。

二.Action测试代码

直接上代码,具体解释见代码注释
代码演示内容:创建Action,Action默认值,Action获取参数
Struts2系列(二)Action_第1张图片

必不可少之主配置文件struts.xml




    <struts>
    
    <package name="hello" namespace="/hello" extends="struts-default">
        
    <action name="helloaction" class="cjx.action.HelloAction" method="hello">
        
         
        <result name="success" >/hello.jspresult>
    action>
    package>
    
    
    <constant name="struts.i18n.encoding" value="UTF-8">constant>
    
    <constant name="struts.action.extension" value="action,,">constant>
    
    <constant name="struts.devMode" value="true">constant>

    
    <include file="cjx/action/UserActionStruts.xml">include>
    <include file="cjx/action/DefaultActionStruts.xml">include>
    <include file="cjx/action/ParamActionStruts.xml">include>
    struts>

必不可少之web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2display-name>
  
  <filter>
  <filter-name>struts2filter-name>
  
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
  <filter-name>struts2filter-name>
  
  <url-pattern>/*url-pattern>
  filter-mapping>

  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
web-app>

HelloAction.java

//import com.opensymphony.xwork2.Action;
//import com.opensymphony.xwork2.ActionSupport;

public class HelloAction {//Struts2的第一个测试
    public String hello(){
        /*搭建struts2框架
         * 1.导包
         * ···\struts-2.3.24\apps\struts2-blank.war是一个空白项目
         * 把空白项目中的包导入即可
         * 2.书写Action类
         * 3.书写src/struts.xml
         * 4.将struts2核心过滤器配置到web.xml
         * 5.测试
         */
        System.out.println("Hello World!You get Struts2!");
        return "success";
    }

}

/*Action类规范
 * 方式1:创建一个类,可以是POJO(不继承父类,不实现接口的类),需要一个公共的无参构造方法和Action方法。
 * Action方法要求:
     * 方法权限修饰符为public
     * 返回一个字符串,指示下一个页面的Result
     * 方法没有参数
 * public String xxx() throws  xxx{
 * return null;
 * }
 * 好处:代码侵入性低(但是使用servlet需要实现它的接口)

 * 方式2:实现一个action接口
 * 里面有execute方法,提供action方法的规范.
 * Action接口预置了一些字符串.可以在返回结果时使用.
public class AboutAction implements Action{
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
}

 *方式3:继承ActionSupport类
 *帮我们实现了 Validateable, ValidationAware, TextProvider, LocaleProvider接口 .
 *如果我们需要用到这些接口的实现时,不需要自己来实现了.
class AboutAction extends ActionSupport{
}
 */

DefaultAction.java

public class DefaultAction extends ActionSupport {//Struts2默认值测试
    public String execute(){
        System.out.println("Struts2默认值测试");
        return "success";
    }
}

DefaultActionStruts.xml



    <struts>    
    <package name="default" namespace="/default" extends="struts-default">      
    
    <default-action-ref name="default">default-action-ref>
    
    <action name="default" class="cjx.action.DefaultAction" >
    <result>/hello.jspresult> 
    action>
    package>  
    struts>

hello.jsp

   Hello World!This  is Struts2!<br>

UserAction.java

public class UserAction {//动态方法调用演示
    public String add(){
        System.out.println("添加用户!");
        return "success";
    }
    public String delete(){
        System.out.println("删除用户!");
        return "success";
    }
    public String update(){
        System.out.println("修改用户!");
        return "success";
    }
    public String find(){
        System.out.println("查找用户!");
        return "success";
    }
}

UserActionStruts.xml



    <struts>
    
    
    
    
    
    <package name="dynamic" namespace="/user" extends="struts-default">
    <action name="useraction*" class="cjx.action.UserAction" 
    method="{1}">
    <result name="success">result>
    action>
    package>
    struts>

ParamActionStruts.xml



    <struts>

<package name="param" namespace="/param" extends="struts-default">
    <action  name="paramaction" class="cjx.action.ParamAction" >
    <result name="success">/form.jspresult>
    action>
    <action name="paramaction2" class="cjx.action.ParamAction2" method="exe">
    <result name="success">/form.jspresult>
    action>
        <action name="paramaction3" class="cjx.action.ParamAction3" method="cute">
    <result name="success">/form.jspresult>
    action>
    package>
    struts>

form.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <body>
  <div style="float: left;">  
<form action="${pageContext.request.contextPath}/param/paramaction" method="post">
方式一:属性驱动和模型驱动<br>
post-name:<input type="text" name="name"><br>
post-age:<input type="text" name="age"><br>
post-date:<input type="text" name="date"><br>
<input type="submit" value="提交">
form><br>

<form action="${pageContext.request.contextPath}/param/paramaction" method="get">
方式一:属性驱动和模型驱动<br>
get-name:<input type="text" name="name"><br>
get-age:<input type="text" name="age"><br>
get-date:<input type="text" name="date"><br>
<input type="submit" value="提交">
form><br>

<form action="${pageContext.request.contextPath}/param/paramaction" method="post">
方式二:对象驱动(注意name值)<br>
name:<input type="text" name="user.name"><br>
age:<input type="text" name="user.age"><br>
date:<input type="text" name="user.date"><br>
<input type="submit" value="提交">
form><br>

<form action="${pageContext.request.contextPath}/param/paramaction2" method="post">
方式三:模型驱动<br>
name:<input type="text" name="name"><br>
age:<input type="text" name="age"><br>
date:<input type="text" name="date"><br>
<input type="submit" value="提交">
form><br>
div>

<div style="float: inherit;">  
<form action="${pageContext.request.contextPath}/param/paramaction3" method="post">
list集合类型封装<br>
list:<input type="text" name="list"><br>
list:<input type="text" name="list[2]"><br>
list:<input type="text" name="list[4]"><br>
list*:<input type="text" name="list[2]"><br>
list*:<input type="text" name="list[4]"><br>
<br>map集合类型封装<br>
map:<input type="text" name="map['one']"><br>
map:<input type="text" name="map['two']"><br>
map:<input type="text" name="map['three']"><br>
map*:<input type="text" name="map['one']"><br>
map*:<input type="text" name="map['one']"><br>
<input type="submit" value="提交">
form><br>
div>
body>
html>

ParamAction.java

//如何获得参数
public class ParamAction extends ActionSupport {
    //方式一:属性驱动
    //准备与参数名称相同的属性和get,set方法
    //每次请求都会创建action对象(原因)
    //自动类型转换只能转换基本数据类型及其包装类和date类型
    private String name;
    private Integer age;
    //date类型只能转换yyyy-MM-dd类型字符串
    private Date date;
    //方式二:对象驱动
    //准备与参数名称相同的实体类和属性和get,set方法
    private User user;


    /*演示代码*/
    public ParamAction() {//构造参数
        System.out.println("Action创建了");
    }
    public String execute() throws Exception {
        System.out.println("方式一:name参数:"+name+";age参数:"+age+";date参数:"+date);
        System.out.println("方式二:"+user);
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
}

ParamAction2.java

public class ParamAction2 extends ActionSupport implements ModelDriven<User>{
        //方式三:模型驱动
        //准备与参数名称相同的属性但没有get,set方法
        //实现接口ModelDriven,T为要封装的对象,实现未实现方法
        private User modeluser=new User();
        @Override
        public User getModel() {//实现接口ModelDriven未实现方法
            //只需要把要封装的对象return 即可
            //但是要注意:属性必须new出要封装的对象,否则会返回null
            return modeluser;
        }
        /*演示代码*/
        public ParamAction2() {//构造参数
            System.out.println("Action创建了");
        }
        public String exe() throws Exception {

            System.out.println("方式三:"+modeluser);
            return SUCCESS;
        }
}

ParamAction3.java

public class ParamAction3 {//集合类型封装

    private List<String> list;
    private Map<String, String>map;
    public String cute() throws Exception {
        System.out.println("list:"+list);
        System.out.println("map:"+map);
        return "success";
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }   
}

你可能感兴趣的:(JAVA-WEB)