资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,Struts2,接收请求参数,章节内容,采用基本类型接收请求参数,3,1,采用复合类型接收请求参数,3,2,ModelDriven,接收请求参数,3,3,关于,struts2.1.6 Action,接收中文请求参数乱码问题,3,4,1,、接收请求参数,2,、采用基本类型接收请求参数,Index.jsp,:,form action=/control/employee/,list_execute.action,method=post,id:,name:,对应的,Action:,public class,HelloWorldAction,/?id=23&name=xxx,private Integer id;,private String name;,public Integer,getId,()return id;,public void,setId(Integer,id),this.id,=id;,public String,getName,()return name;,public void,setName(String,name),this.name,=name;,public String execute()throws Exception,return success;,例,2,Index.jsp,:,添加用户,Struts.xml,:,/,useraddsuccess.jsp,对应的,action,public class,UserAction,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;,public String add(),System.out.println(name,=+name);,System.out.println(age,=+age);,return SUCCESS;,实例,3,HelloWorld.java,文件如下:,public,class,HelloWorldAction,extends,ActionSupport,private,String,msg,;,public,String,getMessage,(),return,msg,;,public,void,setMessage,(String,msg,),this,.msg,=,msg,;,public,String execute(),throws,Exception,msg,=,我的,HelloStruts2,应用程序,;,return,success;,要跳转的,hellostruts2.jsp:,$message,页面上写的是,message,,不是,msg,可以看到,在页面上,直接就得到了,message,的值,也就是说,”,message”,的命名,是由,getMessage,(),方法的,get,后部分决定,而不是由属性名称来决定的。,3,、采用复合类型接收请求参数,(Domain Model),form action=/control/employee/,list_execute.action,method=post,id:,name:,public class,HelloWorldAction,/?id=23&name=xxx,private Person,person,;,public Person,getPerson,()return person;,public void,setPerson(Person,person),this.person,=person;,public String execute()throws Exception,return success;,例,2-1,Index.jsp,:,添加用户,Struts.xml,:,/,user_add_success.jsp,例,2-2(action),public class,UserAction,extends,ActionSupport,private User,user,;,public String add(),System.out.println(name,=+,user.getName,();,System.out.println(age,=+,user.getAge,();,return SUCCESS;,public User,getUser,(),return user;,public void,setUser(User,user),this.user,=user;,对应的,model:(,下页,),例,2-3,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;,对于复合类型接收请求参数,那个类,(,如,Person,类,),,一定要提供无参的构造方法,不然,系统会取不到对应的属性的值。,4,、,ModelDriven,(,模型驱动,),接收请求参数,Index.jsp,:,添加用户,Struts.xml,:,/,useraddsuccess.jsp,实例,(action),public class,UserAction,extends,ActionSupport,implements,ModelDriven,private User,user,=new User();,public String add(),System.out.println(name,=+,user.getName,();,System.out.println(age,=+,user.getAge,();,return SUCCESS;,Override,public User,getModel,(),return user;,注意:,User,为模型对象,关于,struts2.1.6 Action,接收中文请求参数乱码问题,举例,-1,Index.jsp,:,姓名:,举例,-2(Struts.xml),/,user_add_success.jsp,/,user_add_success.jsp,写成这样,传递到,action,中的中文,name,仍然是个乱码,所以说这个是,struts,的,bug.,我们无法通过,struts,解决,只能用新版本更新。或者不用,struts,处理,就用上面介绍的解决,或者,,org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter-,org.apache.struts2.dispatcher.FilterDispatcher,换成,FilterDispatcher,举例,-3(action),public class,UserAction,extends,ActionSupport,private String name;,public String add(),System.out.println(name,=+name);,return SUCCESS;,public String,getName,(),return name;,public void,setName(String,name),this.name,=name;,谢 谢,!,
展开阅读全文