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="https://img-blog.csdn.net/20151107124451402?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
struts2當前台空文字框時 後台action的拍段,不同類型不一樣
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==nullprivate List<String> color;//color.get(0).equals("")private Object [] size;//oject 使用equals("")private int age;//age==0public 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;}