SpringMVC的传递参数方式

1.向构造器传递参数
2.Setter传递参数
===============================
public class Address {
private String Shengfen;
private String city;
private String state;
private String country;
public String getShengfen() {
return Shengfen;
}
public void setShengfen(String shengfen) {
Shengfen = shengfen;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [Shengfen=" + Shengfen + ", city=" + city + ", state=" + state + ", country=" + country + "]";
}
public Address(){}
public Address(String shengfen, String city, String state, String country) {
super();
Shengfen = shengfen;
this.city = city;
this.state = state;
this.country = country;
}


}

===================================================

public class Employee {
private String name;
private int age;
private Address address;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", address=" + address + "]";
}
public Employee(String name, int age, Address address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public Employee(){}
}

============================================================

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean name="Address" class="com.dzh.bean.Address">
<constructor-arg index="0" value="江苏"/> 
<constructor-arg index="1" value="常州"></constructor-arg>
<constructor-arg index="2" value="武进湖塘镇"></constructor-arg>
<constructor-arg index="3" value="延政中路9号江南农村商业银行(金融大厦)"></constructor-arg>
</bean>
<bean name="Employee" class="com.dzh.bean.Employee">
<property name="name" value="董钟淮"></property>
<property name="age" value="24"></property>
<property name="address" ref="Address"></property>
</bean>
</beans>

================================================================================================
package com.dzh.bean;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
//Product product=context.getBean("product", Product.class);
//product.setName("Excellent snake oil");
Employee employee=context.getBean("Employee",Employee.class);

System.out.println("Employee:" +employee.toString());
}
}

=======================================
运行结果:

Employee:Employee [name=董钟淮, age=24, address=Address [Shengfen=江苏, city=常州, state=武进湖塘镇, country=延政中路9号江南农村商业银行(金融大厦)]]


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值