核心提示:Struts2处理表单数据。GreetingAction:package com.home.web.action;import com.opensymphony.xwork2.ActionSuppor...
Struts2处理表单数据。
GreetingAction:
package com.home.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class GreetingAction extends ActionSupport {
/** serialVersionUID. */
private static final long serialVersionUID = 1L;
private String username;
@Override
public String execute() throws Exception {
if (username == null || username.equals("")) {
return ERROR;
}
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"https://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.action.extension" value="html" />
<!-- 声明包 -->
<package name="default" extends="struts-default" namespace="/">
<action name="test_*" class="com.home.web.action.GreetingAction"
method="{1}">
<!-- 添加成功的映射页面 -->
<result name="success">/pages/success.jsp</result>
<!-- 修改成功的映射页面 -->
<result name="error">/pages/error.jsp</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script language="JavaScript" src="JS/AjaxRequest.js"></script> <script language="javascript"> </script> <title>起始页</title> </head> <body> <form action="test_.html" method="post"> 请输入你的姓名:<input type="text" name="username"> <input type="submit" value="提交"> </form> </body> </html>success.jsp:
<%@ page language="java" autoFlush="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>成功提示页面</title> </head> <body> <font color="red"> <s:property value="username" /> </font> ,您好! <br> 欢迎来到本站。 </body> </html>error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>错误提示页面</title> </head> <body> <font color="red">错误:您没有输入姓名!</font> </body> </html>web.xml:
<!-- Struts过滤器 --> <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>


