J2EE中那些忘却的~

2011年12月25日 21:57

1.About Struts2

1.1 The minimal required Struts 2 framework Jar files

  1. commons-fileupload-X.X.X.jar
  2. commons-io-X.X.X.jar
  3. commons-lang-X.X.jar
  4. commons-logging-X.X.X.jar
  5. commons-logging-api.X.X.jar
  6. freemarker-X.X.X.jar
  7. ognl-X.X.X.jar
  8. struts2-core-X.X.X.X.jar
  9. xwork-core-X.X.X.jar
  10. javassist-X.X.X.jar

1.2 To enable the Struts 2 framework to work with your web application you need to add a Servlet filter class and filter mapping to web.xml.

web.xml Servlet Filter
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

Note the url-pattern node value is /* meaning the Struts 2 filter will be applied to all URLs for this web application.

 

1.3 The minimal xml configuration. Note the file name is struts.xml and it should be in the src folder (struts.xml must be on the web application's root class path).

struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<constant name="struts.devMode" value="true" />

	<package name="basicstruts2" extends="struts-default">

		<action name="index">
			<result>/index.jsp</result>
		</action>

	</package>

</struts>

 

1.4 Struts2 use flow

  1. Create a class to store the welcome message (the model)
  2. Create a server page to present the message (the view)
  3. Create an Action class to control the interaction between the user, the model, and the view (the controller)
  4. Create a mapping (struts.xml) to couple the Action class and view

 

 

2.About Log4J

2.1 Download path:    http://archive.apache.org/dist/logging/log4j/

2.2 Setup a log4j.xml configuration in the src folder.

log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
       <layout class="org.apache.log4j.PatternLayout"> 
          <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/> 
       </layout> 
    </appender>
 
    <!-- specify the logging level for loggers from other libraries -->
    <logger name="com.opensymphony">
    	<level value="DEBUG" />
    </logger>

    <logger name="org.apache.struts2">
    	 <level value="DEBUG" />
    </logger>
  
   <!-- for all other loggers log only debug and above log messages -->
     <root>
        <priority value="INFO"/> 
        <appender-ref ref="STDOUT" /> 
     </root> 
    
</log4j:configuration> 

 

 

Tags: java log4j struts
评论(11) 阅读(1603)

MyBatis的种种

2011年12月18日 21:49

最近项目上要用MyBatis做持久层,说实话,还真没听过这么个著名的东西。

去官网(http://www.mybatis.org/)下个jar包和documentation,大体看来和hibernate的应用没差太多。

(说白了就是蛋疼得把Java代码里该写的东西写到XML里去,然后再用Java从XML读到Java里。)

不过不用在DAO层写N多的SQL确实比较省事,毕竟拿String去拼SQL是很不纯粹的事,我这人就是反感不纯粹的东西。

 

MyBatis的配置和hibernat还是比较相近的。有个全局的配置xml,用来配置驱动,url,name和password。

然后对于每个实体类都会有个映射xml,对于这个实体类对应表的操作SQL也就是写在这个xml里的。如果表之间关系很复杂,那么这个xml也将变成噩梦。与这个xml想配对的(当然可以不用,使用其他方式代替,不过相对麻烦)

interface(假设叫TestMapper),定义了对表的基本操作,而这些操作应该是在映射xml里已经实现的SQL。

 

大概配置的结构就这么多,然后就是jar包里提供的Resources把配置xml文件读进来:

Reader reader = Resources.getResourceAsReader(xml_path);

然后用读进来的东西去生成个SqlSessionFactory(用来生成Session的(和JDBC的connection有的一比))

SqlSessionFactory _sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

上述的东西可以写成一个工具类,方便调用。

 

然后可以写DAO层了。

用工具生成个SqlSessionFactory _factory = SqlSessionFactoryUtil.getSqlSessionFactory();

然后在增删改查方法里,用_factory去生成Session

_session = _factory.openSession();

这个Mapper就是和那个实体类映射文件配对的东西,用它的实例去掉它定义的方法。
TestMapper mapper = _session.getMapper(TestMapper.class);
_test = mapper.selectTest(id);
_session.commit();

之后记得commit,他喵的竟然不会自己commit,还得我insert完了之后性喜若狂,结果表里什么都没有。

直接openSession()打开的session没有自动commit,不过可以打开自动commit功能。

 

 

只是顺手写个过程,顺便用来抱怨一下,最好别做为学习参考,还是看看官方的documentation。

 

 

Tags: MyBatis Database JDBC Java
评论(0) 阅读(1921)