您的位置:首页 > 编程语言 > Java开发

《深入浅出struts2》--第七章,类型转换-支持复杂的对象

2015-07-27 16:51 585 查看
struts.xml文件

<action name="Admin1">
<result>/jsp/Admin.jsp</result>
</action>
<action name="Admin2" class="app07a.Admin">
<result name="input">/jsp/Admin.jsp</result>
<result name="success">/jsp/Confirmation.jsp</result>
</action>


web.xml文件

<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>


Action动作类与实体类

package app07a;

import com.opensymphony.xwork2.ActionSupport;

public class Admin extends ActionSupport {
private Employee employee;//struts标签映射的动作类属性是一个类的对象
private String adminId;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getAdminId() {
return adminId;
}
public void setAdminId(String adminId) {
this.adminId = adminId;
}

public String execute(){
return SUCCESS;
}
}


package app07a;

import java.util.Date;

public class Employee {
private String firstName;
private String lastName;
private Date birthDate;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName
+ "]";
}
}


Admin.jsp

<s:form action="Admin2">
<s:textfield name="adminId" label="Admin ID"/>
<s:textfield name="employee.firstName" label="Employee FirstName"/>
<s:textfield name="employee.lastName" label="Employee LastName"/>
<s:textfield name="employee.birthDate" label="Employee BirthDate"/>
<s:submit value="tijiao"/><s:subset/>
</s:form>


特别指出:struts标签可以映射到一个动作类属性的属性。只要像上面那样写既可以。很方便,得益于OGNL

confirmation.jsp

<s:property value="employee.firstName"/>
<s:property value="employee.lastName"/>
<s:date name="employee.birthDate" format="MMM dd,yyyy"/>


自定义类型转换器以及配置

package app07a;

import java.lang.reflect.Member;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.ServletContext;

import ognl.DefaultTypeConverter;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.conversion.TypeConversionException;

public class MyDateConverter extends DefaultTypeConverter {
public Object convertValue(Map context, Object target, Member member, String propertyName,
Object value, Class toType) {

if(toType==Date.class){
ServletContext servletContext=(javax.servlet.ServletContext) context.get(StrutsStatics.SERVLET_CONTEXT);
String datePattern=servletContext.getInitParameter("datePattern");
DateFormat format=new SimpleDateFormat(datePattern);
format.setLenient(false);
try{
String[] s=(String[]) value;
Date date=(Date) format.parse(s[0]);

return date;
}catch(Exception e){
System.out.println("Error:"+e);
throw new TypeConversionException("Invalid conversion");
}
}
return null;
}
}


配置自定义的类型转换器Admin-conversion.properties

birthDate=app07a.MyDateConverter


运行结果:





(提示:上面红色信息,是自定义的错误信息的定制以及Css定制的效果)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: