项目中的一些心得
1.数据库表的命名应该大写,并表达完整的含义,用下划线分开,如T_WEB_XXXXX_USER.这里的XXXXX是指项目名称.表的字段 命名可以是是项目名称首字母缩写加下划线再加具体的有意义的名称,如xx_userId,xx_userName......强烈推荐设计数据库时使用 rational rose,这样便于管理和维护.
2.在struts中,action的路径配置.如果一些项目具有周期性,有些项目可能今年做了,明年还要做类似的,那最好在路径前加上年份,如:
<action path="/2006/xxx/user_add" type="com.web.xxx.action.UserAddAction">
<forward name="success" path="/WEB-INF/jsp/2006/xxx/success.jsp"></forward>
</action>
<forward name="success" path="/WEB-INF/jsp/2006/xxx/success.jsp"></forward>
</action>
3.在做项目前,要做好准备工作,搜集可以用的资源,写设计文档和流程图,只有你心中有谱的时候,你才能考虑的周全,以后即使项目中出现失误的地方,可以先查看文档和流程图,再检查代码,直到错误排除。
在struts中配置中文字符编码。
在struts中配置中文字符编码时,可以写一个类来继承ActionServlet类,重载process方法.
1.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionServlet;
public class ChinaRedTea extends ActionServlet {
private static final long serialVersionUID = -995919609576385793L;
public ChinaRedTea() {
}
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GB2312");
super.process(request, response);
}
}
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GB2312");
super.process(request, response);
}
}
2.修改web.xml文件.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
-->
<web-app xmlns=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
-->
<servlet>
<servlet-name>ChinaRedTea</servlet-name>
<servlet-class>com.chinaredtea.web.blog.actions.ChinaRedTea</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-name>ChinaRedTea</servlet-name>
<servlet-class>com.chinaredtea.web.blog.actions.ChinaRedTea</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ChinaRedTea</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-name>ChinaRedTea</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这样就可以不需要在web应用的action类中进行中文字符转换了。你也可以把一些jsp页面都配置成action,这样整个应用都不用处理中文了。
获取中文unicode编码的方法
/**
* 比如:ch为"中文",将得到"/u4e2d/u6587"
*
* 2006年2月24日。
*
* @param ch
* @return
*/
public static String obUnicode(String ch) {
if (ch == null || ch.equals("")) {
ch = "中文";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ch.length(); ++i) {
sb.append(" //u " + Long.toHexString(ch.charAt(i)));
}
return sb.toString();
}
* 比如:ch为"中文",将得到"/u4e2d/u6587"
*
* 2006年2月24日。
*
* @param ch
* @return
*/
public static String obUnicode(String ch) {
if (ch == null || ch.equals("")) {
ch = "中文";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ch.length(); ++i) {
sb.append(" //u " + Long.toHexString(ch.charAt(i)));
}
return sb.toString();
}