JAVAEE開發過程中遇到的一些問題
1、加入如下jsp檔案頭之後,會遇到一些錯誤。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="page"/>
需要加入
jstl.jar
standard.jar
jsp-api.jar(位於tomcat/lib下)
jsp-2.1-6.1.12.rc4.jar(這個版本才有SetTag類,對應c:set)
2、eclipse 控制檯輸出亂碼
進入eclipse-preferences-general-workspace-text file encoding
根據需要變成UTF-8格式或GBK格式
3、錯誤:createCriteria is not valid without active transaction
解決辦法:
在sessionFactory bean中加入:hibernate.current_session_context_class=thread
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="hibernateProperties"> <value> hibernate.dialect=${hibernate.dialect} hibernate.query.substitutions=true 'Y', false 'N' hibernate.cache.use_second_level_cache=true hibernate.show_sql=${hibernate.show_sql} hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.current_session_context_class=thread </value> <!-- Turn batching off for better error messages under PostgreSQL --> <!-- hibernate.jdbc.batch_size=0 --> </property> </bean>
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
//這裡在事務裡面進行操作
...
tx.commit();
//session.close();
//也可以這樣做:
Session session = sessionFactory.openSession();
//這裡在事務裡面進行操作
....
//session.close();
採用上一種getCurrentSession()方法需要將以下bean註釋掉。
推薦用openSession(),這樣你可以不必註釋transactionManager。
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>
4、action調用不了service層,service層調用不了dao層。
(1)、action調用不了service層方法,看看applicationContext-struts.xml中property有沒有設定正確,且UserManagerImpl.java實現類中有沒有宣告@Service("userManager")
<bean id="userAction" class="net.juntech.action.UserAction" scope="prototype">
<property name="userManager" ref="userManager" />
</bean>
(2)、service層調用不了dao層方法,在UserManagerImpl.java實現類中新增一個構造方法,其中@Autowired不能少,因為我沒並沒有再UserManager.java介面類宣告這個方法。
@Autowired
public UserManagerImpl(UserDao userDao){
super(userDao);
this.userDao=userDao;
}
5、tomcat無法啟動,提示埠被佔用
Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost
are already in use. The server may already be running in another process, or
a system process may be using the port. To start this server you will need
to stop the other process or change the port number(s)
再次啟動tomcat遇到這個錯誤,很奇怪,明明之前已經啟動過一次並且關閉了。
我的系統是OS X 10.9,結合之前每次執行tomcat會有一個java圖示在Dock欄中,我覺得應該是tomcat可能沒有正確退出。去檢視程序,果然有一個叫java的程序在執行。
終端使用killall java或活動管理器(Linux、OS X等系統)、windows控制檯應該有個taskill命令或工作管理員可以解決這個問題。
上面的命令也許殺不掉程序,因為它的父程序是eclipse,可以選擇先關閉eclipse再殺掉這個程序。也可以用killall -9 java強制結束程序。
killall java
killall -9 java
當然,如果還解決不了問題,去嘗試尋找並結束使用這3個埠的程式。
實在不行,就去更改tomcat目錄下cfg/server,xml中8005,8080,8009埠,改成其他未被使用的埠,訪問埠也將有所變化。