Java学习日记_廖万忠
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);