簡単なstrutsの例
index.jsp
プロファイル
struts-config.xml
1、AtionForm
ビジネスロジッククラス
DynaActionFormTestAction.java
正常にページに戻りました
dyna_actionform.jsp
2、strutsアップロードファイル
正常にページに戻りました
upload_success.jsp
3、タイプ変換
正常にページに戻りました
typeconvert_success.jsp
説明する
1、動的ActionForm動的ActionFormは、標準ActionFormの膨張を避けるために設計するものであり、動的ActionFormを用いる標準ActionFormのすべての機能を得ることができる*struts-config.xmlファイルでは、*アクションで動的ActionFormmを使用する動的ActionFormmを定義します.mTestActionjava DynaActionForm daf = (DynaActionForm)form; String username = (String)daf.get("username"); Integer age = (Integer)daf.get("age"); 動的ActionFormは、ページのhtml要素の名前と値をmapに格納するため、getメソッドで対応する値を取り出すことができます.動的ActionFormはEL式の出力方式を採用しています.${dynabean.map.prop}、参照:dyna_actionform.jsp動的Actionの検証は、通常動的検証フレームワークvalidatorを使用します.
2、strutsを用いてファイル*ページをアップロードする構成.例えば、
-
-
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title> ActionForm</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
-
- <body>
- <h1> ActionForm</h1>
- <hr>
- <li> ActionForm</li><br>
- <form action="dyanactionform.do" method="post">
- :<input type="text" name="username"><br>
- :<input type="text" name="age"><br>
- <input type="submit" value=" ">
- </form>
- <p>
- <li> struts </li><br>
- <form action="upload.do" method="post" enctype="multipart/form-data">
- :<input type="text" name="title"><br>
- :<input type="file" name="myfile"><br>
- <input type="submit" value=" ">
- </form>
- <p>
- <li> </li><br>
- <form action="blankfield.do" method="post">
- :<input type="text" name="username"><br>
- <input type="submit" value=" ">
- </form>
- <p>
- <li> ActionForm </li><br>
- <form action="typeconvert.do" method="post">
- intValue:<input type="text" name="intValue"><br>
- doubleValue:<input type="text" name="doubleValue"><br>
- booleanValue:<input type="text" name="booleanValue"><br>
- java.sql.Date:<input type="text" name="sqlDate"><br>
- java.util.Date:<input type="text" name="utilDate"><br>
- <input type="submit" value=" ">
- </form>
- </body>
- </html>
プロファイル
struts-config.xml
-
-
- <?xml version="1.0" encoding="ISO-8859-1" ?>
-
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
- "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
-
- <struts-config>
- <form-beans>
- <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm">
- <form-property name="username" type="java.lang.String"/>
- <form-property name="age" type="java.lang.Integer"/>
- </form-bean>
-
- <form-bean name="uploadForm" type="com.bjsxt.struts.UploadActionForm"/>
-
- <form-bean name="blankFieldForm" type="com.bjsxt.struts.BlankFieldActionForm"/>
-
- <form-bean name="typeconvertForm" type="com.bjsxt.struts.TypeConvertActionForm"/>
- </form-beans>
-
- <action-mappings>
- <action path="/dyanactionform"
- type="com.bjsxt.struts.DynaActionFormTestAction"
- name="dynaForm"
- scope="request"
- >
- <forward name="success" path="/dyna_actionform.jsp"/>
- </action>
-
- <action path="/upload"
- type="com.bjsxt.struts.UploadTestAction"
- name="uploadForm"
- scope="request"
- >
- <forward name="success" path="/upload_success.jsp"/>
- </action>
-
- <action path="/blankfield"
- type="com.bjsxt.struts.BlankFieldTestAction"
- name="blankFieldForm"
- scope="request"
- >
- <forward name="success" path="/blankfield_success.jsp"/>
- </action>
-
- <action path="/typeconvert"
- type="com.bjsxt.struts.TypeConvertTestAction"
- name="typeconvertForm"
- scope="request"
- >
- <forward name="success" path="/typeconvert_success.jsp"/>
- </action>
- </action-mappings>
-
- <controller maxFileSize="10M"/>
-
- <plug-in className="com.bjsxt.struts.UtilDateConverterInitWithPlugin"/>
- </struts-config>
1、AtionForm
-
-
- package com.bjsxt.struts;
-
- import org.apache.struts.action.ActionForm;
-
- /**
- *
- * @author Administrator
- *
- */
- public class BlankFieldActionForm extends ActionForm {
-
- private String username;
-
- private String password;
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
- }
-
-
- package com.bjsxt.struts;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
-
- /**
- *
- * @author Administrator
- *
- */
- public class BlankFieldTestAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- return mapping.findForward("success");
- }
-
- }
-
-
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@ page import="com.bjsxt.struts.*" %>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- BlankFieldActionForm form = (BlankFieldActionForm)request.getAttribute("blankFieldForm");
- %>
- <li>jsp </li><br>
- :<%=form.getUsername() %><br>
- :<%=form.getPassword() %><br>
- <p>
- <li>el </li><br>
- :${blankFieldForm.username }<br>
- :${blankFieldForm.password }<br>
- </body>
- </html>
ビジネスロジッククラス
DynaActionFormTestAction.java
-
-
- package com.bjsxt.struts;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.DynaActionForm;
-
- /**
- * ActionForm
- * @author Administrator
- *
- */
- public class DynaActionFormTestAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- DynaActionForm daf = (DynaActionForm)form;
- String username = (String)daf.get("username");
- Integer age = (Integer)daf.get("age");
- System.out.println("username=" + username);
- System.out.println("age=" + age);
- return mapping.findForward("success");
- }
-
- }
正常にページに戻りました
dyna_actionform.jsp
-
-
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- :${dynaForm.map.username }<br>
- :${dynaForm.map.age }<br>
- </body>
- </html>
2、strutsアップロードファイル
-
-
- package com.bjsxt.struts;
-
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.upload.FormFile;
-
- /**
- * ActionForm
- * @author Administrator
- *
- */
- public class UploadActionForm extends ActionForm {
-
- private String title;
-
- // FormFile
- private FormFile myfile;
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public FormFile getMyfile() {
- return myfile;
- }
-
- public void setMyfile(FormFile myfile) {
- this.myfile = myfile;
- }
- }
-
-
- package com.bjsxt.struts;
-
- import java.io.FileOutputStream;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.upload.FormFile;
-
- /**
- * Action
- * @author Administrator
- *
- */
- public class UploadTestAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- UploadActionForm uaf = (UploadActionForm)form;
- System.out.println("title" + uaf.getTitle());
- FormFile myFile = uaf.getMyfile();
- if (myFile != null) {
- System.out.println("fileName=" + myFile.getFileName());
-
- FileOutputStream fos = new FileOutputStream("c:\\" + myFile.getFileName());
- fos.write(myFile.getFileData());
- fos.flush();
- fos.close();
- }
- return mapping.findForward("success");
- }
-
- }
正常にページに戻りました
upload_success.jsp
-
-
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- :${uploadForm.title}<br>
- :${uploadForm.myfile.fileName }<br>
- </body>
- </html>
3、タイプ変換
-
-
- package com.bjsxt.struts;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import org.apache.commons.beanutils.Converter;
-
- /**
- * java.util.Date
- * @author Administrator
- *
- */
- public class UtilDateConverter implements Converter {
-
- public Object convert(Class type, Object value) {
- System.out.println("UtilDateConverter.value=" + value);
- if (value == null) {
- return value;
- }
- if (value instanceof Date) {
- return value;
- }
- Date d = null;
- if (value instanceof String) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- try {
- d = sdf.parse((String)value);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return d;
- }
-
- }
-
-
- package com.bjsxt.struts;
-
- import java.util.Date;
-
- import javax.servlet.ServletException;
-
- import org.apache.commons.beanutils.ConvertUtils;
- import org.apache.struts.action.ActionServlet;
- import org.apache.struts.action.PlugIn;
- import org.apache.struts.config.ModuleConfig;
-
- public class UtilDateConverterInitWithPlugin implements PlugIn {
-
- public void destroy() {
- }
-
- public void init(ActionServlet servlet, ModuleConfig config)
- throws ServletException {
- System.out.println("UtilDateConverterInitWithPlugin.init()");
- ConvertUtils.register(new UtilDateConverter(), Date.class);
- }
- }
-
-
- package com.bjsxt.struts;
-
- import java.util.Date;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
-
- import org.apache.commons.beanutils.ConvertUtils;
-
- /**
- * java.util.date
- * @author Administrator
- *
- */
- public class UtilDateConverterInitWithServlet extends HttpServlet {
-
- @Override
- public void init() throws ServletException {
- System.out.println("UtilDateConverterInitWithServlet.init()");
- ConvertUtils.register(new UtilDateConverter(), Date.class);
- }
-
- }
正常にページに戻りました
typeconvert_success.jsp
-
-
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- intValue:${typeconvertForm.intValue }<br>
- doubleValue:${typeconvertForm.doubleValue }<br>
- booleanValue:${typeconvertForm.booleanValue }<br>
- java.sql.dateValue:${typeconvertForm.sqlDate }<br>
- java.util.utilValue:${typeconvertForm.utilDate }<br>
- </body>
- </html>
説明する
1、動的ActionForm動的ActionFormは、標準ActionFormの膨張を避けるために設計するものであり、動的ActionFormを用いる標準ActionFormのすべての機能を得ることができる*struts-config.xmlファイルでは、
2、strutsを用いてファイル*ページをアップロードする構成.例えば、