struts2可以自动转换jsp前台传来的信息格式
前台jsp
</pre><pre name="code" class="html"><form action="user_setInfo" method="post"> 名字<input type="text" value="" name="name"/><br/> 日期<input type="text" value="" name="date"/><br/> 年龄<input type="text" value="" name="age"/><br/> 颜色1<input type="text" value="" name="color"/><br/> 颜色2<input type="text" value="" name="color"/><br/> 尺寸1 <input type="text" value="" name="size"/><br/> 尺寸2<input type="text" value="" name="size"/><br/> <input type="submit" value="提交"/> </form>
后台action
package action; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ /** * 952740499 */ private static final long serialVersionUID = 1L; private String name; private Date date; private List<String> color; private String [] size; private int age; public String setInfo() { System.out.println("名字*****"+name); System.out.println("日期*****"+date); System.out.println("颜色*****"+color.get(0)+"*****"+color.get(1)); System.out.println("尺寸*****"+size[0]+"****"+size[1]); System.out.println("年龄******"+age); return "info"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public List<String> getColor() { return color; } public void setColor(List<String> color) { this.color = color; } public String[] getSize() { return size; } public void setSize(String[] size) { this.size = size; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
</pre><pre name="code" class="java">提交后输出
</pre><pre name="code" class="java"><img src="http://img.blog.csdn.net/20151107124451402?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
1.当前台jsp中 input 的那么中 没有后台action的属性名,除int类型外 则接受到判断用 xx==null,int类型用xx==0
2 . 当name和action中属性名匹配时
<span style="color:#ff0000;">//为对象(除Object外)时用xx==null //当为String或Object时用xx.equls("") //当为int时用 xx==0</span> private String name; private Date date;//date==null private List<String> color;//color.get(0).equals("") private Object [] size;//oject 使用equals("") private int age;//age==0 public String setInfo() { System.out.println("名字*****"+name.equals("")); System.out.println(date==null); System.out.println(color.get(0).equals("")); System.out.println("颜色"+color); System.out.println(size[0].equals("")); System.out.println("大小"+size); System.out.println(age==0); return "info"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { System.out.println("ddddddddddddddddddd");//虽然date为null但是会调用 this.date = date; } public List<String> getColor() { return color; } public void setColor(List<String> color) { this.color = color; } public Object[] getSize() { return size; } public void setSize(Object[] size) { this.size = size; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }