1.首先还是创建一个简单Maven的项目,导入jar包,

<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>cn.com</groupId>
<artifactId>ZStructs</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
</dependency> <!-- 导入jstl标签库 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- 导入spring的jar包整合 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency> <!-- 导入spring-struts的整合插件包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.16</version>
</dependency> <!-- 导入jbc的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency> <!-- 导入数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency> <!-- 导入c3p0连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.在web.xml文件下写入spring的相关配置

<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ZStructs</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <filter>
<filter-name>springMVC</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>springMVC</filter-name>
<url-pattern>/*</url-pattern>//拦截所有哦请求
</filter-mapping> <!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

3.在src/main/resources文件夹下创建一个名为:applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 扫描包 -->
<context:component-scan base-package="cn.com.action"></context:component-scan>
<!-- -->
<context:component-scan base-package="cn.com.service"></context:component-scan> <!-- c3p0,数据库连接的 -->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- jabctemplate 用于注入-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0"></property>
</bean> </beans>

4.创建一个entity实体

public class Note implements Serializable {
private int id;
private String context;
private Date publishTime;
private int likeCount;
private int userId;
//get set这里就省略了

5.创建一个service接口:

public interface NoteService {

    public List<Note> selectAll(int userid);

    public int deleteById(int id);
}

6.创建实现类

@Repository//并打上标注
public class NoteIMPL implements NoteService { @Autowired //自动装配
private JdbcTemplate jdbc; @Override
public List<Note> selectAll(int userid) {
String sql = "select * from note where userid=?";
Object[] pss = { userid };
List<Note> list = jdbc.query(sql, pss, new BeanPropertyRowMapper(Note.class));
return list;
} @Override
public int deleteById(int id) {
String sql="delete from note where id= ?";
int update = jdbc.update(sql, id);
return update;
} }

7.创建一个Action

查询列表的Action

@Controller
public class NoteAction { @Autowired
private NoteService se; private List<Note> item; //参数必须有get set 方法 public List<Note> getItem() {
return item;
} public void setItem(List<Note> item) {
this.item = item;
} public String getNoteAll() {
item = se.selectAll(1);
return "suceess";
}
}

删除列表的Action

@Controller
public class DeleteAction { @Autowired
private NoteService se; public String deleteBuId() {
int f = se.deleteById(id);
if (f > 0) {
return "success";
} else {
return "error";
}
} private int id; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} }

8.在src/main/webapp/WEB-INF/demo/下创建一个jsp文件,

<body>
<h1>列表信息</h1>
<table>
<tr>
<th>id</th>
<th>内容</th>
<th>时间</th>
<th>点赞数</th>
<th>用户id</th>
<th>操作</th>
</tr>
<c:forEach items="${item }" var="it">
<tr>
<th>${it.id }</th>
<th>${it.context }</th>
<th>${it.publishTime }</th>
<th>${it.likeCount }</th>
<th>${it.userId }</th>
<th><a href="delete.do?id=${it.id }">删除</a></th>
</tr>
</c:forEach>
</table>
</body>

9最后在src/main/resources文件夹下创建一个struts.xml文件,写一个模版

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<!-- 这里的value可以有多个 -->
<constant name="struts.action.extension" value="do,action"></constant> <package name="demo" extends="struts-default" namespace="/demo"> <!-- noteAction -->
<action name="notelist" class="noteAction" method="getNoteAll">
<result name="suceess" type="dispatcher">/WEB-INF/demr/notelist.jsp
</result>
</action> <!-- delete -->
<action name="delete" class="deleteAction" method="deleteBuId">
<result name="success" type="redirectAction">
<param name="namespace">/demo</param>
<param name="actionName">notelist</param>
</result>
<result name="error" type="dispatcher">/WEB-INF/demr/error.jsp</result>
</action> </package> </struts>

好了,测试大告成功!

Struts2和MVC的简单整合的更多相关文章

  1. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  2. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  3. struts2,hibernate,spring整合笔记(2)

    上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...

  4. SpringMVC入门二: 1规范结构, 2简单整合MyBatis

    昨天拿springMVC写的helloworld结构不好, 这次先调整一下体系结构 , 然后简单整合一下MyBatis spring的配置还是以注解为主, 不过MyBatis的映射文件什么的还是拿xm ...

  5. 第1章-Struts2 概述 --- Struts2和MVC

    (一)Struts2和MVC的关系图: (1)控制器---FilterDispatcher 用户请求首先达到前段控制器(FilterDispatcher).FilterDispatcher负责根据用户 ...

  6. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  7. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  8. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  9. springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能

    转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包     数据库表 数据库表就不用教大家了,一张表,很简 ...

随机推荐

  1. Win(Phone)10开发第(1)弹,桌面和手机的扩展API,还我后退键

    喜大普奔的win10 uap开发预览版终于出了,这次更新跟8.1的变化不是很大,但是将原本win8.1和wp8.1uap的分项目的形式,改为了整合成一个项目,经过一次编译打包成一个appx包,实现了无 ...

  2. 批处理系列(13) -从视频导出高质量GIF图片

    需要ffmpeg,配置ffmpeg到环境变量. 保存代码到HQGIF.bat,与视频同目录,管理员权限运行CMDcd到此目标目录: HQGIF.bat input_video_name.mp4 out ...

  3. POI(java 操作excel,word等)编程

    一.下载所需jar包 下载地址:http://poi.apache.org/download.html http://download.csdn.net/detail/likai22/534250 二 ...

  4. Java基础学习篇---------继承

    一.覆写(重写) 1.含义:子类的定义方法.属性和父类的定义方法.属性相同时候 方法名称相同,参数相同以及参数的个数也相同,此时为覆写(重写) 扩充知识点: 覆盖:只有属性名字和方法名字相同,类型.个 ...

  5. FFmpeg软硬解和多线程解码

    一. AVCodecContext解码上下文 1.avcodec_register_all() : 注册所有的解码器 2.AVCodec *avcodec_find_decoder(enum AVCo ...

  6. Delphi - 10.1编译OSX10.12程序遇到错误解决了!

    昨天,尝试Delphi的跨平台开发功能,在windows10下,做了一个控制台程序,发布目标平台是OSX10.12,中间配置过程都非常顺利,没有任何错误,但是当编译运行时候出现下面错误: [dccos ...

  7. Dockerfile指令详解下

    VOLUME 定义匿名卷 VOLUME指令的格式为: VOLUME [,...] VOLUME 之前我们说过,容器运行时应该尽量保持容器存储层不发生写操作,对于数据库类需要保存动态数据的应用,其数据库 ...

  8. ASP.NET MVC Forms验证机制

    ASP.NET MVC 3 使用Forms身份验证 身份验证流程 一.用户登录 1.验证表单:ModelState.IsValid 2.验证用户名和密码:通过查询数据库验证 3.如果用户名和密码正确, ...

  9. MVC4删除 pages引发的异常 System.Web.Optimization找不到引用

    在MVC4的开发中,如果创建的项目为空MVC项目,那么在App_Start目录下没有BundleConfig.cs项的内容,在手动添加时在整个库中都找不到:System.Web.Optimizatio ...

  10. LODOP内嵌挡住浏览器的div弹出层

    首先,做一个简单的div弹出层用于测试,该弹出层的介绍可查看本博客另一篇博文:[JS新手教程]浏览器弹出div层 然后加入LODOP内嵌,LODOP可以内嵌,C-LODOP不能内嵌,可以在IE等浏览器 ...