作者:互联网发布时间:2010-01-07来源:Java中文网 点我投稿
1.字符长度问题
String s=” 容易出现”;
则s.length()的值为4
s.getBytes().length的值为8
2.对象操作
如果对象tmp为null,则无法对其进行tmp.length(),否则出错。
注意进行判空.
如果字符串长度为8,但是截取长度为10,也抛异常.
3.测试Spring+Hibernate:
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
ProductTypeDAO pdao=(ProductTypeDAO)ctx.getBean("ProductTypeDAO");
4. Spring+Hibernate中applicationContext.xml配置错误,主要由于命名问题:
Error setting property values; nested exception is
org.springframework.beans.NotWritablePropertyException: Invalidproperty 'pTypedao' of bean class[com.Jcuckoo.struts.action.AddPTypeAction]: Bean property 'pTypedao' isnot writable or has an invalid setter method: Does the parameter type ofthe setter match the return type of the getter?
注解:spring默认命名方式不能以双大写字母开头
pTypedao,第一个字母是小写,而第二个字母是大写的属性,它的get和set方法名字不是get+属性第一个字母大写。例如iTestDao的set方法不是setITestDao,可能是setItestDao。所以不建议使用第一个字母是小写,而第二个字母是大写的属性。
另外对应的dao也不能是newsInfodao,我用这个也出现问题,把它改成newInfoDAO解决问题。
5.Struts将控制权限转交到Spring的代码:
struts-config.xml
(1)设置控制转移:
value="org.springframework.web.struts.DelegatingRequestProcessor" />
parameter="com.Jcuckoo.struts.ApplicationResources" />
className="org.springframework.web.struts.ContextLoaderPlugIn">
value="/WEB-INF/classes/applicationContext.xml" />
(2)需要修改
name="loginForm" path="/login" scope="request"
//删除type,控制权交给spring。
type="com.Jcuckoo.struts.action.LoginAction">
(3)修改applicationContext.xml
singleton="false">
6. Struts+Spring+hibernate报错: Hibernate operation: Cannot openconnection; uncategorized SQLException for SQL [???]; SQL state [null];error code [0]; Cannot load JDBC driver class
请在"applicationContext.xml"配置文件中将下面代码
jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test
改成
jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test
千万别笑!格式不同!必须同一行,并且中间不能有空格或其他多余字符!
7.Struct中的Action配置问题
如果直接显示用不到对应的Form,只需要将对应的name属性删掉即可。
刚做了一个程序,从ptype_add.jsp—>addPType.do-->showPType.do-->pTpye_show.jsp显示没有任何问题,但是直接显示showPType.do就不显示,并且控制台不抛异常。原因正如上面分析。
No input attribute for mapping path /SysManager/editPType 并不是说名缺少input属性,而是说,出现错误,无法回跳。
7.下拉选择框
labelProperty="productFactory"/>
其中, products是被显示的集合。Id是Value。productFactory是显示在外面的看的见的信息。
8.文件上传时发生错误:
java.lang.IllegalArgumentException: Cannot invoke com. ProductInfoForm.setFile - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean
这个例子是对的 要特别注意的是 form标签里{或是html:form标签里要设置} enctype="multipart/form-data"
因为form 里默认是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据操作!!!
9.Validation.xml的应用:
(1)对应的JSP
validateProductInfoForm(this);">
(2)validation.xml
(3)struts-config.xml
attribute="productInfoForm"
name="productInfoForm"
parameter="method"
path="/SysManager/productInfo"
scope="request"
validate="true"
input="/SysManager/pinfo_add.jsp"
>
(4)对应的ActionForm
public class ProductInfoForm extends ValidatorActionForm{
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
return super.validate(mapping, request);
}
}
(5)ApplicationResources.properties
errors.productName.required=productName.required
errors.productName.minlength=productName.minlength greater than 6
10.首页重定向:
11.实现两个页面同时刷新:
--------------------------------------------------------------------
onClick="InitRightFrame('editPassword_top.jsp','editPassword.jsp')">密码修改功能
12. 使用bean标签显示时间的格式:
其中的MM要大写!!
13.图片上传:
enctype="multipart/form-data" >
private FormFile file1;
public FormFile getFile1() {
return file1;
}
public void setFile1(FormFile file1) {
this.file1 = file1;
}
public String saveObject(FormFile file){
if (file.getFileName() == null || file.getFileName().equals("")) {
return null;
} else {
String dir = servlet.getServletContext().getRealPath("/newsphoto");
String imageType[] = { "JPG", "jpg", "gif", "bmp", "BMP","pjpeg"};
String fileType = file.getContentType();
int i = fileType.indexOf("/");
fileType = fileType.substring(i + 1);
for (int j = 0; j
if (imageType[j].equals(fileType)) {
String path;
try {
// 调用图片的上传的方法,并且返回上传服务器的路径
path = FileUpLoad.upload(dir, file);
path = "newsphoto/" + path;
return path;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}
return null;
}
public ActionForward addNewsInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
NewInfoForm nf = (NewInfoForm) form;// TODO Auto-generated method stub
NewsInfo news=new NewsInfo();
news.setContent(nf.getContent());
news.setTitle(nf.getTitle());
news.setNewsTime(new Date());
news.setPublishAuthor((String)request.getSession().getAttribute("UserName"));
news.setPicPath1(saveObject(nf.getFile1()));
newInfoDAO.save(news);
return mapping.findForward("AddNewsInfoSuccess");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return mapping.findForward("NewsInfoFail");
}
package util;
public class FileUpLoad {
public static String upload(String dir, FormFile formFile) throws Exception {
Date date = new Date();
String fname = formFile.getFileName();
//取文件的后缀
int i = fname.indexOf(".");
String type = fname.substring(i + 1);
//用时间给文件赋新名称
String name = String.valueOf(date.getTime());
fname = name + "." + type;
InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象
File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象
// 判断指定路径是否存在,不存在则创建路径
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
String path = uploadFile.getPath() + "/" + fname;
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
formFile.destroy();
return fname;
}
}
14.字符串分割
String test="好好学习,天天向上,乐观学习";
String []str=test.split(",");
for(int i=0;i
System.out.println(str[i]);
}
15.隔行变色
方法一:struts 奇偶行不同颜色
bgcolor="">
... ...
方法二:
' ...........
16.分栏效果
信息暂时没有!
{
%>
|
作者:互联网发布时间:2010-01-07来源:Java中文网 点我投稿
1.字符长度问题
String s=” 容易出现”;
则s.length()的值为4
s.getBytes().length的值为8
2.对象操作
如果对象tmp为null,则无法对其进行tmp.length(),否则出错。
注意进行判空.
如果字符串长度为8,但是截取长度为10,也抛异常.
3.测试Spring+Hibernate:
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
ProductTypeDAO pdao=(ProductTypeDAO)ctx.getBean("ProductTypeDAO");
4. Spring+Hibernate中applicationContext.xml配置错误,主要由于命名问题:
Error setting property values; nested exception is
org.springframework.beans.NotWritablePropertyException: Invalidproperty 'pTypedao' of bean class[com.Jcuckoo.struts.action.AddPTypeAction]: Bean property 'pTypedao' isnot writable or has an invalid setter method: Does the parameter type ofthe setter match the return type of the getter?
注解:spring默认命名方式不能以双大写字母开头
pTypedao,第一个字母是小写,而第二个字母是大写的属性,它的get和set方法名字不是get+属性第一个字母大写。例如iTestDao的set方法不是setITestDao,可能是setItestDao。所以不建议使用第一个字母是小写,而第二个字母是大写的属性。
另外对应的dao也不能是newsInfodao,我用这个也出现问题,把它改成newInfoDAO解决问题。
5.Struts将控制权限转交到Spring的代码:
struts-config.xml
(1)设置控制转移:
value="org.springframework.web.struts.DelegatingRequestProcessor" />
parameter="com.Jcuckoo.struts.ApplicationResources" />
className="org.springframework.web.struts.ContextLoaderPlugIn">
value="/WEB-INF/classes/applicationContext.xml" />
(2)需要修改
name="loginForm" path="/login" scope="request"
//删除type,控制权交给spring。
type="com.Jcuckoo.struts.action.LoginAction">
(3)修改applicationContext.xml
singleton="false">
6. Struts+Spring+hibernate报错: Hibernate operation: Cannot openconnection; uncategorized SQLException for SQL [???]; SQL state [null];error code [0]; Cannot load JDBC driver class
请在"applicationContext.xml"配置文件中将下面代码
jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test
改成
jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test
千万别笑!格式不同!必须同一行,并且中间不能有空格或其他多余字符!
7.Struct中的Action配置问题
如果直接显示用不到对应的Form,只需要将对应的name属性删掉即可。
刚做了一个程序,从ptype_add.jsp—>addPType.do-->showPType.do-->pTpye_show.jsp显示没有任何问题,但是直接显示showPType.do就不显示,并且控制台不抛异常。原因正如上面分析。
No input attribute for mapping path /SysManager/editPType 并不是说名缺少input属性,而是说,出现错误,无法回跳。
7.下拉选择框
labelProperty="productFactory"/>
其中, products是被显示的集合。Id是Value。productFactory是显示在外面的看的见的信息。
8.文件上传时发生错误:
java.lang.IllegalArgumentException: Cannot invoke com. ProductInfoForm.setFile - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean
这个例子是对的 要特别注意的是 form标签里{或是html:form标签里要设置} enctype="multipart/form-data"
因为form 里默认是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据操作!!!
9.Validation.xml的应用:
(1)对应的JSP
validateProductInfoForm(this);">
(2)validation.xml
(3)struts-config.xml
attribute="productInfoForm"
name="productInfoForm"
parameter="method"
path="/SysManager/productInfo"
scope="request"
validate="true"
input="/SysManager/pinfo_add.jsp"
>
(4)对应的ActionForm
public class ProductInfoForm extends ValidatorActionForm{
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
return super.validate(mapping, request);
}
}
(5)ApplicationResources.properties
errors.productName.required=productName.required
errors.productName.minlength=productName.minlength greater than 6
10.首页重定向:
11.实现两个页面同时刷新:
--------------------------------------------------------------------
onClick="InitRightFrame('editPassword_top.jsp','editPassword.jsp')">密码修改功能
12. 使用bean标签显示时间的格式:
其中的MM要大写!!
13.图片上传:
enctype="multipart/form-data" >
private FormFile file1;
public FormFile getFile1() {
return file1;
}
public void setFile1(FormFile file1) {
this.file1 = file1;
}
public String saveObject(FormFile file){
if (file.getFileName() == null || file.getFileName().equals("")) {
return null;
} else {
String dir = servlet.getServletContext().getRealPath("/newsphoto");
String imageType[] = { "JPG", "jpg", "gif", "bmp", "BMP","pjpeg"};
String fileType = file.getContentType();
int i = fileType.indexOf("/");
fileType = fileType.substring(i + 1);
for (int j = 0; j
if (imageType[j].equals(fileType)) {
String path;
try {
// 调用图片的上传的方法,并且返回上传服务器的路径
path = FileUpLoad.upload(dir, file);
path = "newsphoto/" + path;
return path;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}
return null;
}
public ActionForward addNewsInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
NewInfoForm nf = (NewInfoForm) form;// TODO Auto-generated method stub
NewsInfo news=new NewsInfo();
news.setContent(nf.getContent());
news.setTitle(nf.getTitle());
news.setNewsTime(new Date());
news.setPublishAuthor((String)request.getSession().getAttribute("UserName"));
news.setPicPath1(saveObject(nf.getFile1()));
newInfoDAO.save(news);
return mapping.findForward("AddNewsInfoSuccess");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return mapping.findForward("NewsInfoFail");
}
package util;
public class FileUpLoad {
public static String upload(String dir, FormFile formFile) throws Exception {
Date date = new Date();
String fname = formFile.getFileName();
//取文件的后缀
int i = fname.indexOf(".");
String type = fname.substring(i + 1);
//用时间给文件赋新名称
String name = String.valueOf(date.getTime());
fname = name + "." + type;
InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象
File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象
// 判断指定路径是否存在,不存在则创建路径
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
String path = uploadFile.getPath() + "/" + fname;
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
formFile.destroy();
return fname;
}
}
14.字符串分割
String test="好好学习,天天向上,乐观学习";
String []str=test.split(",");
for(int i=0;i
System.out.println(str[i]);
}
15.隔行变色
方法一:struts 奇偶行不同颜色
bgcolor="">
... ...
方法二:
' ...........
16.分栏效果
信息暂时没有!
{
%>
|