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.0modelVersion> <groupId>cn.comgroupId> <artifactId>ZStructsartifactId> <version>0.0.1-SNAPSHOTversion> <dependencies> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-coreartifactId> <version>2.5.16version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.0.8.RELEASEversion> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-spring-pluginartifactId> <version>2.5.16version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.0.8.RELEASEversion> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.24version> dependency> <dependency> <groupId>c3p0groupId> <artifactId>c3p0artifactId> <version>0.9.1.2version> dependency> dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-pluginartifactId> <configuration> <source>1.8source> <target>1.8target> 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>ZStructsdisplay-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.htmwelcome-file> <welcome-file>index.jspwelcome-file> <welcome-file>default.htmlwelcome-file> <welcome-file>default.htmwelcome-file> <welcome-file>default.jspwelcome-file> welcome-file-list> <filter> <filter-name>springMVCfilter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class> filter> <filter-mapping> <filter-name>springMVCfilter-name> <url-pattern>/*url-pattern>//拦截所有哦请求 filter-mapping> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:applicationContext.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-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> <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> <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 ListselectAll(int userid); public int deleteById(int id); }
6.创建实现类
@Repository//并打上标注 public class NoteIMPL implements NoteService { @Autowired //自动装配 private JdbcTemplate jdbc; @Override public ListselectAll(int userid) { String sql = "select * from note where userid=?"; Object[] pss = { userid }; List 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 Listitem; //参数必须有get set 方法 public List getItem() { return item; } public void setItem(List 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>idth> <th>内容th> <th>时间th> <th>点赞数th> <th>用户idth> <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> <constant name="struts.action.extension" value="do,action">constant> <package name="demo" extends="struts-default" namespace="/demo"> <action name="notelist" class="noteAction" method="getNoteAll"> <result name="suceess" type="dispatcher">/WEB-INF/demr/notelist.jsp result> action> <action name="delete" class="deleteAction" method="deleteBuId"> <result name="success" type="redirectAction"> <param name="namespace">/demoparam> <param name="actionName">notelistparam> result> <result name="error" type="dispatcher">/WEB-INF/demr/error.jspresult> action> package> struts>
好了,测试大告成功!