Struts自动为对象赋值

java-web交流群:132607763

Action中有对象属性,表单提交后,action自动为对象赋值。
book类:
package com.mm.entity;

import java.util.Date;

public class Book {

	private String name;
	private String author;
	private Date publishDate;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}
	
	public String toString() {
		return this.name + ":" + this.author + ":" + this.publishDate;
	}

}

action类
package com.mm.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import com.mm.entity.Book;

@ParentPackage("struts2Test")
public class BookAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String title;
	private Book book;

	@Action(value = "addBook", results = { @Result(name = SUCCESS, location = "/book.jsp") })
	public String add() {
		System.out.println(book);
		title = "添加成功";
		return SUCCESS;
	}

	@Action(value = "clear", results = { @Result(name = SUCCESS, location = "/book.jsp") })
	public String clear() {
		title = "清空了";
		return SUCCESS;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

}

jsp页面:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sd" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>${title}aa</title>
<sd:head parseContent="true"/>
</head>
<body>
	<s:form action="addBook">
		<s:label value=" 添加图书"></s:label>
		<s:textfield name="book.name" label="书名:"></s:textfield>
		<s:textfield name="book.author" label="作者"></s:textfield>
		<sd:datetimepicker name="book.publishDate" label="出版时间"></sd:datetimepicker>
		<s:submit value="提交"></s:submit>
	</s:form>
</body>
</html>

你可能感兴趣的:(struts2,对象自动赋值)