Java學習日記_廖萬忠
關聯關係(雙向關聯,前向關聯:多對一較多,看錶中的字段,頁面的數據關聯的情況)
實體的類型儘量用包裝類型,防止nll賦值出現異常,基本類型一般用在運算中,效率高。
13. 有用的工具:自定義通用工具,用在不用框架的web開發
CommonUtils:
public class CommonUtils
/返回32位通用唯一標識符,是去掉其中的4個_的
public static String uuid()
return UUID.randomUUID().toString().replace("_","");
/把map轉換為clazz型的bean
public static <T> T toBean(Map map,Class<T> clazz)
try
/通過clazz穿件實例化bean
T bean=clazz.newInstance();
/字符串到日期的轉換
ConvertUtils.register(new DateConverter(),java.util.Date.class);
/構成bean,把map中的數據封裝到bean中
BeanUtils.populate(bean,map);
return bean;
catch(Exception e)
throw new RuntimeException();
public class DateConverter implements Converter
@Override
public Object convert(Class type,Object value)
/如果要轉換的值為空指針,那麼直接返回Null
if(value==null)
return null;
/如果要轉換的值不為字符串,那麼就不轉換了,直接返回對象value
if(!(value instanceof String))
return value;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try
return sdf.parse(value);
catch(ParseException e)
throw new RuntimeException();
2015/8/20
1. HttpServlet:doGet(),doPost(),service();
2. 父類轉子類:父類轉子類是向下轉型,需要強轉,原則:父類型的引用指向的是哪個子類的實例,就能轉向哪個子類的引用。
3. 通用工具類:使用
/用來作為其他Servlet的父類,Struts的Action很像
public class BaseServlet extends HttpServlet throws ServletException,IOException
/覆寫service方法,調用用户想要調用的方法
@Override
public void service(HttpServletRequest request,HttpServletResponse response)
/設置相應編碼
response.setContentType("text/html;charset=utf-8");
/獲取method參數,它是用户想調用的方法
String methodName=request.getMethod("method");
/方法對象
Method method=null;
/通過方法名獲取方法對象
try
method=this.getClass.getMethod(methodName,HttpServletRequest.class
HttpServletResponse.class);
catch(Exception e)
/編譯時異常轉化為運行時異常,不要一味地拋異常,會增加複雜度
throw new RuntimeException(e);
try
/調用用户的方法之後的返回值,這裏是字符串:("f:goods/UserServlet")
String result=method.invoke(this,request,response);
/根據返回結果,決定是否轉發還是重定向,默認為轉發
if(result!=null & !result.isEmpty())
/
int index=result.indexOf(":");
/如果沒有返回值,就默認轉發
if(index=-1)
request.getRequestDispather(result).forward(request,response);
else
/拆分字符串,分割出前綴
String start=result.356lo9095/(0,index);
/要轉發或重定向的路徑
String path=result.substring(index+1);
if("f".equals(start))
/前綴為f",表示轉發
request.getReqeustDispather(path).forward(request,response);
else if("r".equals(start))
/前綴為r",表示重定向
respose.sendRedirect(this.getServletContext+path);
catch(Exception e)
throw new RuntimeException(e);
4. 自定義JdbcUtils
5. JQuery插件:
JQuery_validate
JQuery_complete
JQuery_treeview
fckeditor:富文本編輯器
6. 結構化數據(數據庫) 半結構化數據(xml,html)
7. 驗證碼:防止惡意攻擊,機器人註冊,暴力破解。
Java:一處編譯,到處運行。
×自定義;用AWT(Abstract Window Toolkits,依賴本地的類庫,平台依賴性)實現,有點費勁
用例:VerifyCode
/驗證碼:實體類
public class VerifyCode()
/驗證碼矩形寬高
private int width=70;
private int hight=35;
/隨機數,下面有用
private Random random=new Random();
/驗證碼中的字體類型
private String[] fontNames={};
/矩形框背景色,默認為白色
private Color bgColor=new Color(255,255,255);