Action接受参数的方式
方式1:
<package name="default" namespace="/javaee" extends="struts-default"> <action name="user" class="cn.edu.hpu.action.UserAction"> <result name="success">/User_Add_success.jsp</result> </action> </package>
package cn.edu.hpu.action; public class UserAction { private String name; private int age; public String add(){ System.out.println("name="+name); System.out.println("age="+age); return "success"; } 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; } }
<html> <head> <title>My JSP 'User_Add_success.jsp' starting page</title> </head> <body> User Add success! <br> </body> </html>
<package name="default" namespace="/javaee" extends="struts-default"> <action name="user2" class="cn.edu.hpu.action.UserAction2"> <result name="success">/User_Add_success.jsp</result> </action> </package>
package cn.edu.hpu.mode; 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; } }
package cn.edu.hpu.action; import cn.edu.hpu.mode.User; public class UserAction2 { 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; } }
<html> <head> <title>My JSP 'User_Add_success.jsp' starting page</title> </head> <body> User Add success! <br> </body> </html>
略(就是直接接收一个Form表单对应的实体类)
有关中文参数乱码的问题:
如果不在struts中指定web应用的默认编码集的话,传参时会出现乱码的情况
所以要在struts.xml中设置默认编码集,设置网页的编码方式(下面的例子设置为UTF-8)
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
转载请注明出处:http://blog.csdn.net/acmman/article/details/44158499