在易购网站开发中遇到的问题

搭建SSH环境步骤:

1 搭建spring开发平台
    1.1 添加配置文件和相应的jar包
    1.2创建DATE对象,如果成功Spring的环境OK
    1.3通过基于Spring Junit测试实例化date
    
2 搭建Hibernate的开发环境
    2.1:添加配置文件,和相应的jar包
    2.2: 分析ccnu.shop.util.HibernateSessionFactory的作用:加载Hibernate的配置文件,创建SessionFactory
    2.3:通过表的逆向工程生成model.orm映射文件
    2.4: 创建service,通过new方法实行数据库
    
3 Spring和Hibernate整合(采用AOP来管理事务实现声明式事务)
    3.1: 添加:spring-aop.jar  spring-persistence.jar  c3p0.jar
    3.2: 配置数据源com.mchange.v2.c3p0.ComboPooledDataSource
    3.3: 采用sessionFactory取代HibernateTemplate
    3.4: 配置事务管理器,管理sessionFatory,这样所有的session将会有声明式事务
    3.5: 配置advice(通知),通知是指定了哪些方法需要哪些类型的事务模式
    3.6: 配置AOP切面表达式(通过springframework参考文档参考),pointcut ,aspect等顺序要对
    3.7:让Service交给spring管理,并且依赖sessionFactory,如果能插入数据,则说明声明式事务OK

4.搭建Struts的开发环境
    4.1: 添加配置文件,和相应的hibernate3.6.jar+mysql的驱动包,并且在web.xml中的StrutsPrepareAndExecuteFilter
    4.2: 创建Action,并且配置Struts.xml中
    4.3: 通过jsp访问Action,如果Action可以创建,则代表环境OK
    4.4: 默认在没有整合的时候创建Action的工厂为:(struts) for (com.opensymphony.xwork2.ObjectFactory)

5.创建Spring和struts整合环境()采用Spring来管理Action
    5.1: 添加struts2-spring-plugin-2.3.29.jar
    5.2: 将Action交给spring来管理,配置在/shop/src/applicationContext-action.xml中
    5.3: 在struts.xml中class对应的是spring中配置的id值
    5.4: web.xml中配置监听器ContextLoaderListener,启动时加载spring配置文件


配置AOP切面表达式可以通过springframework参考文档参考


切面配置时pointcut ,aspect,advisor等顺序要对


在myeclipse中导入支持aspectj所需要的jar包时要导入aopalliance.jar aspectj-1.8.6.jar aspectjweaver.jar才行


快捷键的使用

Ctrl + I                   按格式化缩进选中块的代码,用于对齐代码
Ctrl + Shift + F       格式化文档,对整个文档进行格式化处理,包括缩进,行对齐,单词之间的空格
Alt + ↓                当前行向下移动一行
Alt + ↑                当前行向上移动一行
Ctrl + Alt + ↑       复制当前行到上一行(复制增加)
Ctrl + Alt + ↓       复制当前行到下一行(复制增加)
Ctrl + O              快速显示类的结构:类所属的包,类中所有的变量和方法
Ctrl + T               快速显示当前类的继承结构
Ctrl + Shift + M       在文件头加入Import语句
Ctrl + Shift + O       加入缺少的Import语句,删除多余的Import语句

将user library中的jar包导入到tomcat中

    右键进入项目属性 Properties 点进去
    左边 MyEclipse -> Deployment Assembly
    右边 Add -> Java Build Path Entries -> Next
    选择你已经导入的 User Library -> Finish

用struts的建议,就是引用的jar我们就直接选择在网上下载的struts的包解压后的struts-blank里面的jar,一般就没有问题,如果是使用SSH整合开发,就再加一个struts2-spring-plugin-2.1.6.jar,问题一般就会解决。

在spring的web项目中常常会在tomcat启动的时候出现这种提示:
引用
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

网上有好多解决办法都不管用,这个提示应该是读入web应用程序的log4j.properties文件之前就报出来了。是在加载org.springframework.web.context.ContextLoader这个listener的时候没找到log4j的配置文件造成的。

仔细查看web.xml发现在加载org.springframework.web.context.ContextLoader这个listener之后才加载org.springframework.web.util.Log4jConfigListener,把log4j的配置放到org.springframework.web.context.ContextLoader之前,就可以解决这个问题了。
<context-param>  
	<param-name>log4jConfigLocation</param-name>  
	<param-value>/WEB-INF/log4j.properties</param-value>  
</context-param>  
  
<context-param>  
	<param-name>log4jRefreshInterval</param-name>  
	<param-value>60000</param-value>  
</context-param>  
<listener>  
	<listener-class>  
		org.springframework.web.util.Log4jConfigListener  
	</listener-class>  
</listener>  
<!-- end -->  

<listener>  
	<listener-class>  
		org.springframework.web.context.ContextLoaderListener  
	</listener-class>  
</listener>  


然后配置/WEB-INF/log4j.properties,更改数据库信息

对于save,update,delete这些很多重复使用的方法我们可以抽取出来,抽取有接口级别和类级别的抽取

MyEclipse 中自动安插作者、注释日期等快捷键方法

www.MyException.Cn  网友分享于:2013-12-23  浏览:82次
MyEclipse 中自动插入作者、注释日期等快捷键方法

MyEclipse 中自动插入作者、注释日期等de快捷键方法依次打开然后找到

Window -->Preferences->Java->Editor->Templates ,
在这里new一个自己的插入注释的快捷方式名称,具体设置如下:
Name处输入 remark  (任意你喜欢的名称)
context处选 java 后边勾选Automatically insert复选框
Description 这里可以任意输入描述
Pattern :(自定义格式)
/**
* @author yeshiwu
* @date : ${date} ${time}
* @Description : ${todo}
*/
到这里全部设置完毕,下面 OK,找个java类,找个空白处测试一下,       输入remark(前面写的那个Name名称),
        再alt+/         应该按一下就能看到刚才的Description的内容。
注意如果前面context 选 javaDoc的话 就要alt+/ 再按下/才能出来

框架+约定 优于 配置

struts核心代码:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
	//创建ActionContext对象
	prepare.createActionContext(request, response);	
}

public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
	//创建值栈对象
	ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
	//把后面的Map复制到值栈的Map中
	stack.getContext().putAll(dispatcher.createContextMap(request, response, null));
	//把值栈中的map中存储到ActionContext中
	ctx = new ActionContext(stack.getContext());
	return ctx;
}

public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
		ActionMapping mapping) {
	//封装了传入的内置对象	
	Map requestMap = new RequestMap(request);
	Map params = new HashMap(request.getParameterMap());
	Map session = new SessionMap(request);
	Map application = new ApplicationMap(servletContext);
	//把相应的Map,和传入的内置对象交给createContextMap方法
	Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response);
	if (mapping != null) {
		extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
	}
	return extraContext;
}

public HashMap<String,Object> createContextMap(Map requestMap,
								Map parameterMap,
								Map sessionMap,
								Map applicationMap,
								HttpServletRequest request,
								HttpServletResponse response) {
	//声明的大Map对象,存储了传入的所有小Map和Jsp内置对象,此大Map返回							
	HashMap<String,Object> extraContext = new HashMap<String,Object>();
	extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
	extraContext.put(ActionContext.SESSION, sessionMap);
	extraContext.put(ActionContext.APPLICATION, applicationMap);

	Locale locale;
	if (defaultLocale != null) {
		locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
	} else {
		locale = request.getLocale();
	}

	extraContext.put(ActionContext.LOCALE, locale);

	extraContext.put(StrutsStatics.HTTP_REQUEST, request);
	extraContext.put(StrutsStatics.HTTP_RESPONSE, response);
	extraContext.put(StrutsStatics.SERVLET_CONTEXT, servletContext);

	// helpers to get access to request/session/application scope
	extraContext.put("request", requestMap);
	extraContext.put("session", sessionMap);
	extraContext.put("application", applicationMap);
	extraContext.put("parameters", parameterMap);

	AttributeMap attrMap = new AttributeMap(extraContext);
	extraContext.put("attr", attrMap);

	return extraContext;
}

ModelDrivenInterceptor源码:
public class ModelDrivenInterceptor extends AbstractInterceptor {

    protected boolean refreshModelBeforeResult = false;

    public void setRefreshModelBeforeResult(boolean val) {
        this.refreshModelBeforeResult = val;
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
	//获取action
        Object action = invocation.getAction();
	//判断当前action是否实现了ModelDriven接口
        if (action instanceof ModelDriven) {
	    
            ModelDriven modelDriven = (ModelDriven) action;
            ValueStack stack = invocation.getStack();
	    //调用getModel方法,把对象进行压栈
            Object model = modelDriven.getModel();
            if (model !=  null) {
            	stack.push(model);
            }
            if (refreshModelBeforeResult) {
                invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
            }
        }
        return invocation.invoke();
    }

    /**
     * Refreshes the model instance on the value stack, if it has changed
     */
    protected static class RefreshModelBeforeResult implements PreResultListener {
        private Object originalModel = null;
        protected ModelDriven action;


        public RefreshModelBeforeResult(ModelDriven action, Object model) {
            this.originalModel = model;
            this.action = action;
        }

        public void beforeResult(ActionInvocation invocation, String resultCode) {
            ValueStack stack = invocation.getStack();
            CompoundRoot root = stack.getRoot();

            boolean needsRefresh = true;
            Object newModel = action.getModel();

            // Check to see if the new model instance is already on the stack
            for (Object item : root) {
                if (item.equals(newModel)) {
                    needsRefresh = false;
                    break;
                }
            }

            // Add the new model on the stack
            if (needsRefresh) {

                // Clear off the old model instance
                if (originalModel != null) {
                    root.remove(originalModel);
                }
                if (newModel != null) {
                    stack.push(newModel);
                }
            }
        }
    }
}

后台不能直接访问jsp页面,必须通过action转发


在HQL里面查询的from表名要是你映射的对应实体类的名称,而不是你数据库里面的表名。。。。。。所以大小写要注意

先读xml文件,然后依次实例化单例,有构造方法调用构造方法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值