要求:显示主页面,主页面中的按钮提交表单显示所有用户的信息
整体项目结构图如下:
导包:
配置文件
web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>spring_struts-mybatis-work</display-name>
<filter>
<display-name>StrutsPrepareAndExecuteFilter</display-name>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器, 在Web服务器启动时候初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</context-param>
</web-app>
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package namespace="/wq" name="wq" extends="struts-default">
<!-- 显示主页面 -->
<action name="login">
<result>/WEB-INF/login.jsp</result>
</action>
<action name="find" class="testAction" method="testCase">
<result name="success">/WEB-INF/success.jsp</result>
</action>
</package>
</struts>
spring-web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 使用组件扫描 -->
<context:component-scan base-package="cn.tedu"/>
<util:properties id="config" location="classpath:conf/db.properties"/>
<!-- 配置连接池 spring关闭之前关闭连接池-->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="username" value="#{config.username}"/>
<property name="password" value="#{config.password}"/>
<property name="driverClassName" value="#{config.driver}"/>
<property name="url" value="#{config.url}"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcp"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置mapper扫描 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入要扫描的包名,多个包之间使用逗号或分号进行分隔 -->
<property name="basePackage" value="cn.tedu.dao"/>
</bean>
</beans>
Dao接口:
package cn.tedu.dao;
import java.util.List;
import cn.tedu.entity.User;
public interface UserDao {
public List<User> findAllUser();
}
Mapper映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.tedu.dao.UserDao">
<select id="findAllUser" resultType="cn.tedu.entity.User">
SELECT * FROM cn_user
</select>
</mapper>
Service层接口:
package cn.tedu.service;
import java.util.List;
import cn.tedu.entity.User;
public interface UserService {
//查询所有用户
public List<User> findUserList();
}
Service层实现类
package cn.tedu.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource(name="userDao")
private UserDao dao;
//查询所有用户
public List<User> findUserList() {
List<User> list = dao.findAllUser();
return list;
}
}
控制器Action
package cn.tedu.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.tedu.base.BaseAction;
import cn.tedu.entity.User;
import cn.tedu.service.UserService;
@Controller
@Scope("prototype")
public class TestAction extends BaseAction{
private static final long serialVersionUID = 6216045713049692710L;
private List<User> list;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Resource(name="userService")
private UserService service;
public String testCase(){
//调用service层
list = service.findUserList();
return SUCCESS;
}
}
login.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>spring struts2 mybatis</title>
</head>
<body>
<h1>主页面</h1>
<form action="find" method="post">
<input type="submit" value="查询所有用户">
</form>
</body>
</html>
success.jsp:
<%@ 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>
<html>
<head>
<meta charset="utf-8">
<title>spring struts2 mybatis</title>
</head>
<body>
<h1>用户列表</h1>
<table style="width: 100%">
<tr><td>用户ID</td><td>用户姓名</td><td>用户密码</td><td>用户昵称</td></tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.cn_user_id}</td>
<td>${user.cn_user_name}</td>
<td>${user.cn_user_password}</td>
<td>${user.cn_user_nick}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
在浏览器访问URL:http://localhost:8000/spring_struts-mybatis-work/wq/login
如下显示:
点击按钮,浏览器URL:http://localhost:8000/spring_struts-mybatis-work/wq/find
显示如下页面: