Struts2 Pojo(六)

注意:附件中有完整案例
1.采用POJO对象的方法进行赋值和传值
2.web配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <filter>
 	<filter-name>struts2</filter-name>
 	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>

   <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2.struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!--==================== -->
	<!--=====定义常量========= -->
	<!--==================== -->
	
	<!-- 修改请求的后缀名为.do 修改后每个请求必须要有后缀了-->
	<constant name="struts.action.extension" value="do,action"/>
	
	<!-- 默认的视图主题 <constant name="struts.ui.theme" value="simple"/>-->
	<!--+===================+-->
	<!--+=====定义基本包=======+-->
	<!--+===================+-->
	<package name="default" extends="struts-default">
		<action name="showPerson" class="com.luob.action.ShowPerson">
			<result name="success">/showPerson.jsp</result>
		</action>
	</package>
	
</struts>

3.Action
package com.luob.action;

import com.luob.model.Person;
import com.opensymphony.xwork2.ActionSupport;

public class ShowPerson extends ActionSupport {

	private Person person;  //采用POJO的方式进行赋值和存值

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}

}


4.POJO 对象
package com.luob.model;

public class Person {
	private String name;
	private String sex;
	private String age;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}


6.赋值jsp页面
  <body>
   <center>
   	<s:form action="showPerson" method="post">
   		<s:textfield name="person.name" label="姓名"/>
   		<s:textfield name="person.sex"  label="性别"/>
   		<s:textfield name="person.age" label="年龄"/>
   		<s:textfield name="person.address" label="住址"/>
   		<s:submit value="提交"/>
   	</s:form>
   </center>
  </body>


7.取值jsp页面
<body>
   	<center>
   		<h3>使用POJO</h3><br/>
   		<hr>
   		姓名:${person.name}
   		性别:${person.sex}
   		年龄:${person.age}
   		地址:${person.address}
   	</center>
  </body>

你可能感兴趣的:(POJO,strust2)