应用Struts2处理表单数据 博客分类: Struts2 应用Struts2处理表单数据struts2form表单action
程序员文章站
2024-02-25 08:18:52
...
应用Struts2处理表单数据,做如下效果图项目:
关键代码如下所示:
<?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>MyInputName</display-name> <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> </web-app>
package com.yuan; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static final long serialVersionUID = -5863435861595037127L; private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String execute() throws Exception{ if(username==null || "".equals(username)){ return ERROR; }else{ return SUCCESS; } } }
<?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="user" extends="struts-default"> <action name="userAction" class="com.yuan.UserAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<%@ 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>index</title> </head> <body> <form action="userAction.action" method="post"> please input name: <input type="text" name="username"/> <input type="submit" value="submit"/> </form> </body> </html>
<%@ 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"> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>success</title> </head> <body> Hello, <font color="red"> <s:property value="username"/> </font> <br> welcome to the Website! keep improving!!! </body> </html>
<%@ 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>error</title> </head> <body> <font color="red">There is something wrong!please input your name!</font> </body> </html>