在做字串處理的時候都使用String,
如果你的字串處理需要常常變換長度和內容的話,
String s=""; for(int i=1;i<=a;i++){ s+="2"; }
String s=""; for(int i=1;i<=a;i++){ s+="2"; }
for(int i=1;i<=b;i++){ time=0; for(int j=1;j<=i;j++){ if(i%j==0){ time++; if(time>2){ break; }else if(j==i){ System.out.println(i+" "); } } } }
public interface Action { public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; public String execute() throws Exception; }
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable
@Override public void validate() { if(getAccount().length()==0){ addFieldError("account", "請輸入account"); } if(getPassword().length()==0){ addFieldError("password","請輸入password"); } }
<result name="input">/index.jsp</result>跳回index.jsp,以上簡單的驗證就完成了,當然,除了把錯誤訊息放在code中,我們也可以放在一個屬性文件.properties中,以便日後的維護和管理方便。
account.required=請輸入account password.required=請輸入password
addFieldError("account", "請輸入account");
修改成
addFieldError("account", getText("account.required"));
就能讀取到了。 <?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> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings> <action name="loginAction" class="user.action.UserLoginAction" method="login"> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
<html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <s:form name="login" action="loginAction" > <s:textfield name="account" label="account" /> <s:password name="password" label="password" /> <tr> <td> </td> <td><s:submit value="login" theme="simple" /> <s:reset value="reset" theme="simple" /></td> </tr> </s:form> </body> </html>
<html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <s:form > <tr> <th>account</th> <th>password</th> </tr> <tr> <td><s:property value="account" /></td> <td><s:property value="password" /></td> </tr> </s:form> </body> </html>
package user.action; import com.opensymphony.xwork2.ActionSupport; public class UserLoginAction extends ActionSupport{ private String account; private String password; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ return "success"; } }
此篇介紹使用POI套件建立excel檔案