欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2 用ModelDriven接收参数

程序员文章站 2022-06-23 16:31:13
1、struts.xml

1、struts.xml

<struts>
 <constant name="struts.devmode" value="false"> 
 <constant name="struts.enable.dynamicmethodinvocation" value="true">
 <constant name="struts.i18n.encoding" value="utf-8"></constant>
 <package name="user" extends="struts-default">                 
        <action name="user" class="com.wxh.action.useraction">
            <result>/useraddsuccess.jsp</result>
        </action>         
  </package>     
</constant></constant></struts>
2、useraction.java
package com.wxh.action;

import com.wxh.model.user; 
import com.opensymphony.xwork2.actionsupport;
import com.opensymphony.xwork2.modeldriven;


public  class useraction extends actionsupport implements modeldriven{	
	private static final long serialversionuid = 1l;
	 
	private user user=new user();
	
	public string add(){
		system.out.println("name="+user.getname());
		system.out.println("age="+user.getage());
		return success;
	}
	public user getmodel(){
		return user;
	}
	
}

 

 

3、user.java

 

package com.wxh.model;

public class user {
	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;
	}
}
4、index.jsp

添加用户
Struts2 用ModelDriven接收参数