Struts2-内置类型转换

项目结构以及相应jar包见下图:

1、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2-test-2-zh</display-name>
  <filter>
  	<filter-name>Struts2-test-2-zh</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>Struts2-test-2-zh</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2、新建jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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=UTF-8">
<title>用户注册</title>
</head>
<s:head theme="xhtml"/>
<sd:head parseContent="true"/>
<body>
	<s:form action="register.action" method="post">
		<s:textfield name="name" label="用户名称"></s:textfield>
		<s:textfield name="age" label="用户年龄"></s:textfield>
		<sd:datetimepicker name="birthday" label="出生日期"></sd:datetimepicker>
		<sd:textarea cols="20" rows="10" name="intro" label="用户介绍"></sd:textarea>
		<s:submit value="注册"></s:submit>
	</s:form>
</body>
</html>

3、新建action

/*
 *@Author swxctx
 *@time 2016年9月29日
 */
package com.sw.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
	private String name;
	private int age;
	private Date birthday;
	private String intro;
	
	public RegisterAction() {
		// TODO Auto-generated constructor stub
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getIntro() {
		return intro;
	}
	public void setIntro(String intro) {
		this.intro = intro;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println(name+","+age+","+birthday+","+intro);
		return "success";
	}
}

4、struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
<struts>
	
	<package name="Strute2-test-1" extends="struts-default">
	
		<action name="register" class="com.sw.action.RegisterAction">
			<result name="success">/result.jsp</result>
		</action>
	
	</package>
	
</struts>

5、结果页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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=UTF-8">
<title>注册成功-用户信息</title>
</head>
<body>
	用户名称:<s:property value="name"/><br/>
	用户年龄:<s:property value="age"/><br/>
	出生日期:<s:date name="birthday" format="yyyy-MM-dd" nice="true"/><br/>
	用户介绍:<s:property value="intro"/>
</body>
</html>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Swxctx

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值