步骤:
1.创建一个form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/form.action"><!--注意后面的action-->
用户名:<input type="text" name = "name"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
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> <package name="form180627" namespace="/" extends="struts-default" > <action name="form" class="com.struts.c_form.Demo7Action" method="execute" > <result name="SUCCESS" type="dispatcher" >/form.jsp</result><!-- type="dispatcher" 默认为转发 redirect:重定向--> </action> </package> </struts>
3.创建action接收类:
package com.struts.c_form; import com.opensymphony.xwork2.ActionSupport; /** * 表单提交 * @author taoyulong * */ public class Demo7Action extends ActionSupport{ //准备与参数键名称相同的属性 private String name; public String execute() throws Exception { System.out.println("name参数值:"+name); return "SUCCESS"; } //-----------接收前台数据的必要条件----------------------- public String getName() {// return name; } public void setName(String name) { this.name = name; } }