天天看點

S2SH架構搭建與項目結構分析HTTP Status404 - No result defined for action XXXX and result success 處理

1 使用mysql ,檢查是否安裝mysql

2 啟動mysql

3 myeclipse 連接配接mysql,配置資訊以及驅動檔案導入

https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&tn=monline_3_dg&wd=myeclipse%E4%B8%AD%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93&oq=myeclipse%E4%B8%AD%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93&rsv_pq=fac5622100022aed&rsv_t=426bo3%2BmtTuvSeoFzKjCRuHdV7E715nQbp%2FU9vq%2FPA47HzWrAtxydakAM7xDVQ5iAKip&rsv_enter=1&inputT=1498&rsv_sug3=21&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_sug4=3039

HTTP Status404 - No result defined for action XXXX and result success 處理

http://stackoverflow.com/questions/17757947/struts-2-http-status-404-no-result-defined-for-action

http://blog.csdn.net/zhqingyun163/article/details/6394621

Unable to instantiate Action, loginAction,defined for 'login' in namespace

1

http://bbs.csdn.net/topics/380074729

java.lang.NoClassDefFoundError:org.hibernate.cfg.Configuration

1spring和her整合,确認spring jar包有導入,沒有導入的話會提示類找不到。

2整合時不僅需要spring包,還需要配置檔案相應的her包

3引入新的jar包時,需要從新部署項目,但重新開機tomcat沒有。

http://wanglihu.iteye.com/blog/1897718

常見s2sh問題

type Exception report

message

description The server encountered aninternal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

        com.sanqing.action.LoginAction.execute(LoginAction.java:42)

        sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)

問題可以看出是指使用了null對象的屬性或方法

1 在java中定義的變量聲明就是定義,而方法聲明後還需要new,或者指派

Private string str;  這裡由于是變量,其實是string類型,與底層有關,聲明就是定義

Private fuction fun;  這裡隻是聲明了方法,并沒有定義,是以還需要new或者指派

User user = newUser();

    publicvoidsetUserService(UserService userService){

        this.userService=userService;

    }

第一種是new ,第二種是指派,這裡要注意了,這裡是通過參數傳遞一個這樣的方法周遊來指派,那麼這個參數必須有效。這是在s2sh中常見的一種形式,其原因是application.xml中定義了很多bean代理執行個體化方法,也就是注入的方式了,是以上面依然報null錯誤是因為沒有配置application.xml。

S2SH架構搭建與項目結構分析

相應的可以選擇javaee版本,筆者使用預設的5.0。

2struts2

添加struts2支援,同樣可以選擇相應的版本。

筆者選擇相應的版本與配置檔案中形式。

選擇相應的lib包,其中core裡面就包含了很多jar,而且core裡面的不是項目都需要的。其他的lib,筆者也不清楚,不選擇。

可見,core lib包裡面我們不是所有的jar都需要,簡單的支援需要一下幾個包

⊃2;        Commons-logging-1.0.4.jar

⊃2;       Freemarker-2.3.13.jar

⊃2;       Ognl-2.6.11.jar

⊃2;       Struts2-core-2.1.6.jar

⊃2;       Xwork-2.1.2.jar

此時,我們需要在web.xml中為struts2配置相應的資訊,指定struts的位置和作用。

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appversion="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name></display-name>

 <welcome-file-list>

   <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 <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></web-app>

這裡不再解釋web.xml配置說明。

2 hibernate

連接配接資料庫的

指定hibernateSessionFactory檔案的,這個是hibernate執行資料庫操作的必須建立的檔案,後面用spring後,那麼spring為我們建立這個檔案。

完成hibernate後,項目結構如上。

這裡要實作hibernate架構,同樣lib包時必須的,

導入最小jar包,即使用Hibernate3所必需的jar包。下載下傳hibernate-distribution-3.3.1.GA解壓後,必需jar包都在lib"required目錄下。必需jar包如下:

⊃2;       hibernate3.jar-----------------------------核心類庫

⊃2;       antlr-2.7.6.jar-----------------------------代碼掃描器,用來翻譯HQL語句

⊃2;       commons-collections-3.1.jar----------- Apache Commons包中的一個,包含了一些Apache開發的集合類,功能比java.util.*強大

⊃2;       dom4j-1.6.1.jar----------------------------是一個Java的XML API,類似于jdom,用來讀寫XML檔案的

⊃2;       javassist-3.4.GA.jar----------------------- Javassist 位元組碼解釋器

⊃2;       jta-1.1.jar------------------------------------标準的JTAAPI。

⊃2;       slf4j-api-1.5.2.jar

⊃2;       slf4j-nop-1.5.2.jar

Hibernate.cfg.xml配置

<?xmlversion='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configurationPUBLIC

         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

   <session-factory>

       <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>

       <property name="connection.url">jdbc:mysql://localhost:3306/test</property>

       <property name="connection.username">root</property>

       <property name="connection.password">myazi</property>

       <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>

       <propertyname="myeclipse.connection.profile">test</property>

   </session-factory>

</hibernate-configuration>

配置不再解釋,需要注意是必須有連結資料庫的驅動包

3 Spring

選擇相應的版本,

添加spring後檔案結構如上,錯誤的原因是

<?xmlversion="1.0" encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <beanid="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <propertyname="configLocation"

            value="classpath:hibernate.cfg.xml">

        </property>

    </bean></beans>

缺少spring.orm.hibernate3..的jar包,需要自己添加。添加後注意myeclpse需要儲存才能不顯示叉。

引入spring架構後需要修改web.xml檔案

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appversion="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name></display-name>

 <welcome-file-list>

   <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 <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>

   <!--指定strust2的action交給spring-->

     <listener>

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

   </listener>

   <!--指定application。xml的路徑-->

   <context-param>

 <param-name>contextConfigLocation</param-name>

 <param-value>classpath*:applicationContext*.xml</param-value>

</context-param>

  </web-app>

由上步驟下來,部署伺服器啟動報如下錯誤

2016-3-10 15:08:07 org.apache.catalina.core.AprLifecycleListenerinit

資訊:The Apache Tomcat Native library which allows optimal performance in productionenvironments was not found on the java.library.path:D:\myEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin;D:\myEclipse\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201211011550\tomcat\bin

2016-3-10 15:08:07org.apache.coyote.http11.Http11Protocol init

資訊:Initializing Coyote HTTP/1.1 on http-8080

2016-3-10 15:08:07org.apache.catalina.startup.Catalina load

資訊:Initialization processed in 409 ms

2016-3-10 15:08:07org.apache.catalina.core.StandardService start

資訊:Starting service Catalina

2016-3-10 15:08:07org.apache.catalina.core.StandardEngine start

資訊:Starting Servlet Engine: Apache Tomcat/6.0.13

2016-3-10 15:08:08org.apache.catalina.core.StandardContext listenerStart

嚴重:Error configuring application listener of classorg.springframework.web.context.ContextLoaderListener

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    atorg.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)

    atorg.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)

    atorg.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3770)

    atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4334)

    atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)

    atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)

    atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)

    atorg.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)

    atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)

    atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)

    atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)

    atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)

    atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)

    atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)

    atorg.apache.catalina.core.StandardHost.start(StandardHost.java:719)

    atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)

    atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)

    atorg.apache.catalina.core.StandardService.start(StandardService.java:516)

    atorg.apache.catalina.core.StandardServer.start(StandardServer.java:710)

    atorg.apache.catalina.startup.Catalina.start(Catalina.java:566)

    atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)

    atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    atjava.lang.reflect.Method.invoke(Method.java:597)

    atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)

    atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

2016-3-10 15:08:08org.apache.catalina.core.StandardContext listenerStart

嚴重:Skipped installing application listeners due to previous error(s)

2016-3-10 15:08:08org.apache.catalina.core.StandardContext start

嚴重:Error listenerStart

2016-3-10 15:08:08org.apache.catalina.core.StandardContext start

嚴重:Context [/hh] startup failed due to previous errors

2016-3-10 15:08:08org.apache.coyote.http11.Http11Protocol start

資訊:Starting Coyote HTTP/1.1 on http-8080

2016-3-10 15:08:08org.apache.jk.common.ChannelSocket init

資訊:JK: ajp13 listening on /0.0.0.0:8009

2016-3-10 15:08:08org.apache.jk.server.JkMain start

資訊:Jk running ID=0 time=0/26  config=null

2016-3-10 15:08:08org.apache.catalina.startup.Catalina start

資訊:Server startup in 1392 ms

缺少org.springframework.web.context.ContextLoaderListener的jar包在,這個在spring lib包裡面,添加spring 包就行,注意一定要重新删除後部署才有用,隻要是添加新的包,就要删除原來部署在伺服器上的項目,重新部署啟動。

好了終于部署完項目了,但是有沒有發現部署這個項目下來不知道導入了多少包了,但是并不是之前導入的包我們都需要,是以這裡我們一些必要的包進行說明下。

如下:

這裡不再詳細介紹,缺少相應的包,筆者犯的錯誤後面會說明。

2 搭建好S2SH架構後,我們還需要建立項目,項目的結構很重要,同樣在搭建項目結構時也需要配置相應的東西。

Action 負責顯示層的送出資料和現實資料。

Service 負責把action收集到的資料整理起來,并向背景申請相應的服務和背景處理完服務傳回的結果交給action。

Dao 負責連接配接資料庫,對service的請求做資料操作和把資料操作的結果傳回給service。

Po 負責建立資料庫的表和java變量的關系,建立關系配置由XXX.hbm.Xml配置。

Util 負責一些常用的功能函數的實作。

Action和jsp action和name對應,result必須的有,沒有配置spring的話就class全路徑,配置spring的話就與spring中bean的id一緻。

Web.xml配置

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appversion="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name></display-name>

 <welcome-file-list>

   <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 <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>

  <!--指定strust2的action交給spring-->

     <listener>

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

   </listener>

   <!--指定application。xml的路徑-->

   <context-param>

 <param-name>contextConfigLocation</param-name>

 <param-value>classpath*:applicationContext*.xml</param-value>

</context-param>

 </web-app>

Strust.xml配置

<struts><!-- 根節點 -->

    <constantname="struts.i18n.encoding" value="utf-8"></constant>

    <packagename="struts2" extends="struts-default" namespace="/">

        <actionname="login" class="loginAction">

            <resultname="success" >user.jsp</result>

            <resultname="input">index.jsp</result>

            <resultname="admin">admin.jsp</result>

        </action>

        <actionname="register" class="registerAction">

            <resultname="success">/admin.jsp</result>

            <resultname="input">/user.jsp</result>

        </action>

        </package>

</struts>

Spring配置

<?xmlversion="1.0" encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- 定義資料源-->

    <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <propertyname="driverClassName">

            <value>com.mysql.jdbc.Driver</value><!--設定驅動類 -->

        </property>

        <propertyname="url">

            <value>jdbc:mysql://localhost:3306/oj</value><!--設定連接配接URL -->

        </property>

        <propertyname="username">

            <value>root</value>                     <!-- 資料庫使用者名-->

        </property>

        <propertyname="password">

            <value>myazi</value>                    <!-- 資料庫密碼-->

        </property>

    </bean>

        <!--定義SessionFactory -->

    <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <propertyname="dataSource">            <!--資料源-->

            <refbean="dataSource"/>

        </property>

        <propertyname="hibernateProperties">   <!--Hibernate配置屬性-->

            <props>

                <propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <propkey="hibernate.show_sql">false</prop>

            </props>

        </property>

        <propertyname="mappingResources">      <!--關系映射檔案-->

            <list>

                <value>com/sanqing/po/Message.hbm.xml</value>

                <value>com/sanqing/po/Problem.hbm.xml</value>

                <value>com/sanqing/po/Reply.hbm.xml</value>

                <value>com/sanqing/po/Title.hbm.xml</value>

                <value>com/sanqing/po/User.hbm.xml</value>

            </list>

        </property>

    </bean>

    <!--定義HibernateTemplate -->

    <beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate">

        <propertyname="sessionFactory">

            <refbean="sessionFactory"/>        <!--注入SessionFactory執行個體對象-->

        </property>

    </bean>

    <beanid="hibernateTransactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <propertyname="sessionFactory">

            <refbean="sessionFactory"/>    <!--注入SessionFactory執行個體對象-->

        </property>

    </bean>

    <beanid="userDAOTarger" class="com.sanqing.dao.UserDAOImpl"><!--裝配資料通路層-->

        <propertyname="hibernateTemplate">

            <refbean="hibernateTemplate"/> <!--注入HibernateTemplate -->

        </property>

    </bean>

    <beanid="userDAO"

         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

        <propertyname="transactionManager">

            <refbean="hibernateTransactionManager"/><!-- 為事務代理bean注入事務管理器-->

        </property>

        <propertyname="transactionAttributes"><!-- 設定事務屬性 -->

            <props>

            <!--所有的方法,采用required的事務政策 -->

            <propkey="*">PROPAGATION_REQUIRED</prop>

            </props>

        </property>

        <propertyname="target"><!--為事務代理bean設定目标bean -->

            <refbean="userDAOTarger"/>

        </property>

    </bean>

    <beanid="userService" class="com.sanqing.service.UserServiceImpl">

        <propertyname="userDAO" ref="userDAO"></property><!--注入DAO層-->

    </bean>

    <beanid="loginAction" class="com.sanqing.action.LoginAction"scope="prototype" autowire="byName">

        <propertyname="userService" ref="userService"></property><!--注入業務邏輯層-->

    </bean>

        <beanid="registerAction"class="com.sanqing.action.RegisterAction"scope="prototype">

    </bean>

    </beans>

http://blog.csdn.net/yaerfeng/article/details/27175679

http://wanglihu.iteye.com/blog/1897718