Cust案例总结
一、
以下是工厂类,实现使用配置文件关联接口类与实现类
学习过程中有许多的错误出现在这了,代码需要好好的理解。
/**
* 通用工厂类
* @author ABRAM
*
*/
public class BasicFactory {
private static BasicFactory factory = new BasicFactory();
private static Properties prop = null;
private BasicFactory() {}
//返回一个对象,单例模式
public static BasicFactory getFactory(){
return factory;
}
//得到配置文件的访问路径
static{
try {
prop = new Properties();
//反射取得路径
prop.load(new FileReader(BasicFactory.class.getClassLoader().getResource("config.properties").getPath()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//通用返回类示例 ,?(通过配置文件返回实现类)
public <T> T getInstance(Class<T> clazz){ //例子CustDao.class
try {
String cName = clazz.getSimpleName();//"CustDao"
String cImplName = prop.getProperty(cName);//"com.itheima/dao.CustDaoImpl"
return (T)Class.forName(cImplName).newInstance();//生成类的示例强转为T泛型
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
二、问题描述:mysql数据库中文乱码
解决办法:http://www.cnblogs.com/donqiang/articles/2057972.html
三、c标签的使用
1、问题描述:在修改页面时要显示用户选择过的checkbox.
解决办法:引用
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
在checkbox中的代码如下(if条件判断,使用fn函数):
<input type="checkbox" name="preference" value="篮球"
<c:if test="${fn:contains(cust.preference,'篮球')}">checked='checked'
</c:if>/>篮球
2、问题描述:解决script语句注入问题。
解决办法:加入<c:out/>
标签
代码:<td><c:out value="${cust.name }"/></td>