照黎活明的视频教程写的SSH2集成,SPRING与HIBERNATE部分没有问题,已经在JUNIT中测试过。
集成STRUTS2后,打开页面报404错误,找不到问题所在。下面贴出代码。
[code="web.xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
指定SPRING的配置文件,默認從WEB根目錄尋找配置文件, 我們可以通過SPRING提高的classpath:前綴指定從該路徑下尋找
-->
contextConfigLocation
classpath:beans.xml
<!--
對SPRING容器實例化,實例放入APPLICATION範圍.
實例化後的容器會讀取內容參數context-param設置的SPRING配置文件
-->
org.springframework.web.context.ContextLoaderListener
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
[/code]
[code="struts.xml"]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- 設置由SPRING提供的對象工廠類接替STRUTS2的對象工廠類的工作,用於創建ACTION -->
<package name="employee" namespace="/employee" extends="struts-default">
<action name="list" class="employeeAction">
<result name="list">/WEB-INF/page/employee.jsp</result>
</action>
</package>
[/code]
[code="EmployeeAction"]
package cn.lyq.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.lyq.service.EmployeeService;
@Controller //employeeAction 標識控制層,交給SPRING管理
public class EmployeeAction {
@Resource EmployeeService employeeService;
public String execute(){
ActionContext.getContext().put("employees", employeeService.list());
return "list";
}
}
[/code]