web.xml
struts.xml
pom.xml
applicationContext.xml
action类
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<!--在这之前要先导入maven dependency坐标 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!--ctrl+左键有显示下划线表示导入成功 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts2 核心过滤器 -->
<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> |
<?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>
<package name="default" namespace="/" extends="struts-default">
<!-- class对应applicationContext.xml的界面层的bean id名-->
<action name="index" class="categoryAction" method="list">
<result>/index.jsp</result>
</action>
</package>
</struts> |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>samchen</groupId>
<artifactId>mycinema</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 写下面这个properties的好处是当使用springframework其他jar包时可以使用
${springframework.version}
-->
<properties>
<springframework.version>4.3.1.RELEASE</springframework.version>
</properties>
<dependencies>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 建议先导入Spring的相关坐标在导入Struts的相关坐标 -->
<!-- Spring DI 容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>
${springframework.version}
</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Struts 2.3.20 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.20</version>
</dependency>
<!-- Struts2整合Spring插件 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project> |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- id为接口,class为实现类 -->
<!-- 数据访问层 -->
<bean id="categoryDao" class="mycinema.dao.impl.CategoryDaoImpl" />
<!-- 业务逻辑层 -->
<bean id="categoryBiz" class="mycinema.biz.impl.CategoryBizImpl" >
<!-- 依赖注入
name 为实现类的属性 -->
<property name="categoryDao" ref="categoryDao"></property>
</bean>
<!-- 界面层 Struts2 Action -->
<!--
scope
=
"prototype" 表示非单例,默认是单例的
-->
<bean id="categoryAction" scope="prototype" class="web.action.CategoryAction">
<property name="categoryBiz" ref="categoryBiz"/>
</bean>
|
package web.action;
import java.util.List;
import mycinema.biz.CategoryBiz;
import com.opensymphony.xwork2.ActionSupport;
public class CategoryAction extends ActionSupport {
private CategoryBiz categoryBiz;
public void setCategoryBiz(CategoryBiz categoryBiz) {
this.categoryBiz = categoryBiz;
}
private List<String> categoryList;
public List<String> getCategoryList() {
return categoryList;
}
public String list(){
this.categoryList = categoryBiz.getAll();
return SUCCESS;
}
}
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul>
<c:forEach var="c" items="${categoryList }">
<li>${c}</li>
</c:forEach>
</ul>
</body>
</html> |
SSH整合抛异常:action未发现。
检查步骤:
1、看看你struts.xml文件中action中对应的class属性的值是否可以在Spring的配置文件中找到对应的id值。
2、如果你确定你的配置没有出现问题,那么一定是你导入包的时候少了包,这时候你检查下你的项目中是否有以下三个jar包,struts2-spring-plugin-2.0.11.1.jar、commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar。