struts2笔记(续一)

Struts2中接受参数(一):

 

StrudentAction.java:

package com.oristand.test;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class StudentAction extends ActionSupport {

    private String name;

    private int age;

 

    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;

    }

 

 

    @Override

    public String execute() throws Exception {

       // TODO Auto-generated method stub

       return SUCCESS;

    }

   

   

    public String add(){

       System.out.println("name is: " + name );

       System.out.println("age is: " + age );

       return SUCCESS;

    }

   

    public String delete(){

       return SUCCESS;

    }

   

 

}

 

Struts.xml:

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

        <action name="*_*" class="com.oristand.test.{1}Action" method="{2}" >

        <result>/{1}_{2}.jsp</result>

       </action>

 </package>

 

客户端访问:

http://localhost:8888/struts2_0100_ActionConfig/Student_add?name=gaoge&age=24

则在console下打印出来:

name is: gaoge

age is: 24

 

Struts2中接受参数(二):利用domainModel

StudentAction.java

package com.oristand.test;

 

import com.opensymphony.xwork2.ActionSupport;

import com.oristand.vo.Person;

 

public class StudentAction extends ActionSupport {

    private String name;

    private int age;

   

    private Person person;

 

    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;

    }

 

 

    @Override

    public String execute() throws Exception {

       // TODO Auto-generated method stub

       return SUCCESS;

    }

   

   

    public String add(){

       System.out.println("name is: " + name );

       System.out.println("age is: " + age );

       System.out.println("person's name is: " + person.getName() );

       System.out.println("person's age is: " + person.getAge() );

       return SUCCESS;

    }

   

    public String delete(){

       return SUCCESS;

    }

 

 

    public Person getPerson() {

       return person;

    }

 

 

    public void setPerson(Person person) {

       this.person = person;

    }

   

 

}

 

Person.java:

package com.oristand.test;

 

import com.opensymphony.xwork2.ActionSupport;

import com.oristand.vo.Person;

 

public class StudentAction extends ActionSupport {

    private String name;

    private int age;

   

    private Person person;//这个person不需要new struts2会自动new 出来一个

 

    public String getName() {

       return name;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值