方式1:jsp表单数据填充到action中的属性;
方式2:jsp表单数据填充到action的对象的属性;
方式1:
第一步:引包,省去
第二步:配置struts2的过滤器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>struts2_20170221</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- struts2过滤器 --> 14 <filter> 15 <!-- 过滤器名称 --> 16 <filter-name>struts2</filter-name> 17 <!-- 过滤器类 --> 18 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 19 </filter> 20 <!-- struts2过滤器映射 --> 21 <filter-mapping> 22 <!-- 过滤器名称 --> 23 <filter-name>struts2</filter-name> 24 <!-- 过滤器映射 --> 25 <url-pattern>/*</url-pattern> 26 </filter-mapping> 27 28 </web-app>
第三步:开发Action
1 package com.bie.type; 2 3 import java.util.Date; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 /** 8 * @author BieHongLi 9 * @version 创建时间:2017年2月21日 下午8:39:13 10 * Struts2的核心业务,请求数据自动封装和类型转换 11 * 这个继承不继承即可extends ActionSupport,习惯继承了 12 */ 13 public class TypeAction extends ActionSupport{ 14 15 16 private static final long serialVersionUID = 1L; 17 private String name; 18 private String password; 19 private int age; 20 private Date birthday;//Date日期类型,导入的utilsl类型的 21 22 //普通的成员变量,必须给set,get可以不给的。 23 public String getName() { 24 return name; 25 } 26 public String getPassword() { 27 return password; 28 } 29 public int getAge() { 30 return age; 31 } 32 public Date getBirthday() { 33 return birthday; 34 } 35 36 37 //处理注册请求,String类型的,不能带参数的 38 public String register() { 39 System.out.println(name+" "+password+" "+age+" "+birthday); 40 return SUCCESS; 41 } 42 }
第四步:配置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="finalPackage" extends="struts-default"> <action name="requestAction" class="com.bie.finalImpl.FinalAction"> <result name="success">success.jsp</result> </action> <action name="ImplAction" class="com.bie.finalImpl.ImplAction"> <result name="success">success.jsp</result> </action> <action name="type_*" class="com.bie.type.TypeAction" method="{1}"> <result name="success">show.jsp</result> </action> </package> </struts>
第五步:注册页面index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>表单提交的页面</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/type_register.action" method="post"> 11 账号:<input type="text" name="name"/> 12 密码:<input type="password" name="password"/> 13 年龄:<input type="text" name="age"/> 14 出生:<input type="text" name="birthday"/> 15 <input type="submit" value="注册"/> 16 </form> 17 </body> 18 </html>
第六步:显示页面show.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>显示的页面</title> 8 </head> 9 <body> 10 <h1>Struts2的进行数据封装和类型转换的使用</h1> 11 </body> 12 </html>
时间: 2024-10-15 06:08:15