打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SSH开发网上商店过程中遇到的16个问题

SSH开发网上商店过程中遇到的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)设置控制转移:

 

<controller>

<set-property property="processorClass"

value="org.springframework.web.struts.DelegatingRequestProcessor" />

</controller>

<message-resources

parameter="com.Jcuckoo.struts.ApplicationResources" />

<plug-in

className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation"

value="/WEB-INF/classes/applicationContext.xml" />

</plug-in>

 

(2)需要修改

<action-mappings>

<action attribute="loginForm" input="/login.jsp"

name="loginForm" path="/login" scope="request"

//删除type,控制权交给spring。

type="com.Jcuckoo.struts.action.LoginAction">

<forward name="success" path="/success.jsp" />

<forward name="fail" path="/fail.jsp" />

</action>

</action-mappings>

 

(3)修改applicationContext.xml

<bean name="/login" class="com.Jcuckoo.struts.action.LoginAction"

singleton="false">

<property name="testdao">

<ref bean="TestDAO" />

</property>

</bean>
 

 

 

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"配置文件中将下面代码

<property name="url">

<value>

jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test

</value>

</property>

 

改成

 

<property name="url">

<value>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test</value>

</property>

 

千万别笑!格式不同!<value></value>必须同一行,并且中间不能有空格或其他多余字符!

 

7.Struct中的Action配置问题

<action attribute="PTypeForm" name="PTypeForm" path="/SysManager/showPType" scope="request" >

<forward name="ShowPTypeSuccess" path="/SysManager/ptype_show.jsp" />

</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.下拉选择框

<html:select property="productCata">

<html:options collection="products" property="id"

labelProperty="productFactory"/>

</html:select>

 

其中, 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

<html:html locale="true">

<html:javascript formName="productInfoForm"/>

<html:form action="/SysManager/productInfo?method=addProductInfo"method="post" enctype="multipart/form-data" onsubmit="return

validateProductInfoForm(this);">

<html:text property="productName" size="65"/>

<html:errors property="productName"/>

</html:form>
 

(2)validation.xml

<formset>

<form name="productInfoForm">

<field property="productName" depends="required,minlength">

<arg key="errors.productName.required" />

<msg name="minlength" key="errors.productName.minlength"/>

<var>

<var-name>minlength</var-name>

<var-value>6</var-value>

</var>

</field>

</form>

</formset>
 

(3)struts-config.xml

<form-bean name="productInfoForm" type="com.Jcuckoo.struts.form.ProductInfoForm" />

 

<action

attribute="productInfoForm"

name="productInfoForm"

parameter="method"

path="/SysManager/productInfo"

scope="request"

validate="true"

input="/SysManager/pinfo_add.jsp"

>

<forward name="ProductInfoFail" path="/SysManager/opError.jsp" />

<forward name="EditProductInfoSuccess" path="/SysManager/opError.jsp" />

<forward name="AddProductInfoSuccess" path="/SysManager/opSuccess.jsp" />

</action>
 

(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.首页重定向:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<logic:redirect href="CUser/index.jsp"></logic:redirect>

 

11.实现两个页面同时刷新:

<script type="text/javascript">

function InitRightFrame(topLocation,rightLocation){

window.parent.frames[0].location=topLocation;

window.parent.frames[1].location=rightLocation;

}

</script>

--------------------------------------------------------------------

<a href="#" class="aa"

onClick="InitRightFrame('editPassword_top.jsp','editPassword.jsp')">密码修改功能</a>
 

 

12. 使用bean标签显示时间的格式:

<bean:write name="newsinfo" property="newsTime" format="yyyy-MM-dd"/>

其中的MM要大写!!

 

13.图片上传:

 

<html:form action="/SysManager/newsInfo.do " method="post"

enctype="multipart/form-data" >

<html:file property="file1" size="50"/>

</html:form>
 

 

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 < imageType.length; 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<str.length;i++){

System.out.println(str[i]);

}

 

15.隔行变色

方法一:struts 奇偶行不同颜色
<logic:notEmpty name="pro_list">
<logic:iterate id="listq" name="pro_list" indexId="index">
<tr
bgcolor="<%=(index.intValue() % 2 == 0) ? "#FFFFF9" : "#FFFFC9"%>">

<td>
<bean:write name="listq" property="t1" />
</td>

... ...

</tr>
</logic:iterate>
</logic:notEmpty>

方法二:

<logic:iterate indexId="index" ..............
<tr class='css<%=index.intValue()%2%>' ...........

16.分栏效果

<logic:notPresent name="productInfo" scope="request">

信息暂时没有!

</logic:notPresent>

<table width="96%">

<tr height="110">

<logic:present name="productInfo" scope="request">

<logic:iterate id="productpic" name="productInfo" scope="request" indexId="i">

<logic:present name="productpic">

 

<td class="Myright1">

<a href="showProductByCata.do?method=getProductById&id=${productpic.id}">

<img src="../${productpic.productImage}" width="95" height="95"/>

</a>

</td>

<% if(i.intValue()%2==1)

{

%>

</tr><tr height="110">

<%}%>

</logic:present>

</logic:iterate>

</logic:present>

</tr>

</table>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Struts标签
struts标签之浅入深出 OBS [和讯博客]
欢迎光临 - 琳婕小筑-老猫的理想 - Struts配置说明 -
Struts1.x系列教程(7):Logic标签库
Struts文件上传
Struts标记库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服