搭建第⼀个web项⽬:实现⽤户的增删改查
好久没写这个web的代码了,再不写估计以前的也忘记的差不多了,赶紧复习下,顺便把⾃⼰的想法慢慢实现了。
最近在公司发现以前的项⽬有⽤easyUI搭建的。赶紧copy回来学习下。
感觉写⽇志真的很有必要,⾃⼰总结的肯定要⽐每次从⽹上查阅来的靠谱。每次搭建⼀个基本的web项⽬都都要查ssh的配置真的很忧伤啊,有⽊有
把jar包的截图截了⼀下,要不总是记不清该⽤到的基本jar有啥。
其中Struts中⽤到的jar有:commons-fileupload.jar commons-io.jar freemarker.jar ognl.jar struts2-core.jar xwork-core.jar
因为⽤到的是Struts的注解形式,所以加⼊了Struts的插件jar:struts2-convention-plugin.jar
hibernate中⽤到的jar有:hibernate核⼼包:hibernate.jar hibernate必须包:antlr.jar commons-collection.jar dom4j.jar javassist.jar jta.jar slf-4j.jar 数据库连接池:c3p0 数据库驱动jdbc:mysql-connector.jar
spring中⽤到的jar有:核⼼包:spring.jar AOP:aspectjrt.jar aspectjweaver.jar 动态代理(和aop也有关)cglib.jar ⽇志有关commons-logging.jar
其他的插件包需要的时候⼀个⼀个添加,上⾯的是最基本的。
好了,这回基本jar包也记录下来了。下⾯开始好好研究下公司项⽬,实现最基本的⽤户增删改查了。
现在最⿇烦的就是项⽬⾥的基础通⽤类不知道该怎么部署,啊啊啊啊啊
马丹,把项⽬拷到公司电脑上之后包了个这个错误,我也是醉了好不,明明都是⼀样的MySQL和Eclipse,为啥就不⾏了呢。荒废了我⼀下午也知道错在哪了。⽹上说事MySQL版本太低了,或是dialect没写对,可是改了也是不⾏啊,真⼼不知道哪⾥错了,只能回家看看原先的数据库是设置的什么字符集了,或许是MyEclipse中的某些字符集设置导致的问题吧,啊啊啊蛋疼。。。
17:33:20,483 WARN JDBCExceptionReporter:233 - SQL Error: 0, SQLState: null
17:33:20,486 ERROR JDBCExceptionReporter:234 - Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')
17:33:20,488 WARN SettingsFactory:147 - Could not obtain connection to query metadata
org.apachemons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')
at org.apachemons.ateDataSource(BasicDataSource.java:1225)
at org.apachemons.Connection(BasicDataSource.java:880)
at Connection(LocalDataSourceConnectionProvider.java:82)
spring framework是什么框架的at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:114)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
at wSessionFactory(LocalSessionFactoryBean.java:814)
at hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
at hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.Object(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at t.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at t.fresh(AbstractApplicationContext.java:380)
at org.ateWebApplicationContext(ContextLoader.java:255)
at org.t.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.tInitialized(ContextLoaderListener.java:45)
at org.StandardContext.listenerStart(StandardContext.java:3827)
at org.StandardContext.start(StandardContext.java:4334)
at org.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.ContainerBase.addChild(ContainerBase.java:771)
at org.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.ContainerBase.start(ContainerBase.java:1053)
at org.StandardHost.start(StandardHost.java:719)
at org.ContainerBase.start(ContainerBase.java:1045)
at org.StandardEngine.start(StandardEngine.java:443)
at org.StandardService.start(StandardService.java:516)
at org.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at flect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: ptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
wInstance0(Native Method)
wInstance(NativeConstructorAccessorImpl.java:39)
wInstance(DelegatingConstructorAccessorImpl.java:27)
at wInstance(Constructor.java:513)
sql.jdbc.Util.handleNewInstance(Util.java:411)
sql.Instance(Util.java:386)
sql.ateSQLException(SQLError.java:1052)
sql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
sql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
sql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
sql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
sql.SQL(ConnectionImpl.java:2618)
sql.figureClientCharacterSet(ConnectionImpl.java:1880)
sql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3499)
sql.tOneTryOnly(ConnectionImpl.java:2384)
sql.ateNewIO(ConnectionImpl.java:2153)
sql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
sql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
wInstance0(Native Method)
wInstance(NativeConstructorAccessorImpl.java:39)
wInstance(DelegatingConstructorAccessorImpl.java:27)
at wInstance(Constructor.java:513)
sql.jdbc.Util.handleNewInstance(Util.java:411)
sql.Instance(ConnectionImpl.java:381)
sql.t(NonRegisteringDriver.java:305)
at org.apachemons.ateConnection(DriverConnectionFactory.java:38)
at org.apachemons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apachemons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apachemons.ateDataSource(BasicDataSource.java:1221)
... 50 more
2015-6-15 17:33:22 http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-6-15 17:33:22 org.apache.jkmon.ChannelSocket init
信息: Port busy 8009 java.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
2015-6-15 17:33:22 org.apache.jkmon.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8010
2015-6-15 17:33:22 org.apache.jk.server.JkMain start
信息: Jk running ID=1 time=0/11 config=null
2015-6-15 17:33:22 org.apache.catalina.startup.Catalina start
信息: Server startup in 4954 ms
蛋疼的,回家这个项⽬根本没有数据库字符集的问题。先不管这个问题了。这章可是要写实现数据的增删改查啊。项⽬中⽤到了easyUI前台框架,写⼀下后台action向前台返回jason格式的代码:
1package cn.itcast.oa.action;
2
3import javax.annotation.Resource;
4
5import org.vention.annotation.Action;
6import org.vention.annotation.Namespace;
7import org.vention.annotation.ParentPackage;
8import org.vention.annotation.Result;
9import org.vention.annotation.Results;
10
11import cn.itcast.oa.base.BaseAction;
12import cn.itcast.oa.domain.EasyGridData;
13import cn.itcast.oa.domain.Role;
14import cn.itcast.oa.service.RoleService;
15
16 @Namespace("/jsp/role")
17 @ParentPackage("json-default")
18 @Action(value = "/roleAction")
19 @Results({
20 @Result(name = "success", type = "json",params = { "root", "gridData" })
21 })
22public class RoleAction extends BaseAction<Role> {
23
24private static final long serialVersionUID = 1L;
25
26 @Resource
27private RoleService roleService;
28
29private EasyGridData<Role> gridData = new EasyGridData<Role>();
30 @Override
31public String execute() throws Exception {
32// roleList = roleService.findAll();
33 gridData.setRows(roleService.findAllByPage(this.ws)) ;
34 gridData.setTotal( roleService.findAll().size());
35// Context().put("roleList", roleList);
36return "success";
37
38 }
39
40public EasyGridData<Role> getGridData() {
41return gridData;
42 }
43public void setGridData(EasyGridData<Role> gridData) {
45 }
46
47 }
其中,封装了⼀个EasyGridData类,该类是为了实现easyUI数据表格分页。EasyGridData代码如下:
1package cn.itcast.oa.domain;
2
3import java.util.ArrayList;
4import java.util.List;
5/**
6 * 实现easyUI数据表格分页
7 * @author haojiahong
8 *
9 * @param <T>
10*/
11public class EasyGridData<T> {
12private int total=0;
13private List<T> rows = new ArrayList<T>();
14private String msg;
15public List<T> getRows() {
16return rows;
17 }
18
19public void setRows(List<T> rows) {
21 }
22
23
24public int getTotal() {
25return total;
26 }
27
28
29public void setTotal(int total) {
31 }
32
33
34public String getMsg() {
35return msg;
36 }
37
38
39public void setMsg(String msg) {
40this.msg = msg;
41 }
42 }
为什么要封装⼀个这样的类呢,因为在⼀开始没有分页的情况时,我直接返回roleList,easyUI表格照样可以解析数据,将返回到前台的jason串显⽰出来。可是当加⼊分页后,返回到前台的数据串编程⼀个类似⼆维数组的东西,roleList⾥⾯的数据,easyUI显⽰不出来了,封装这个类之后吧List中的数据放到了rows中,easyUI⼜可以显⽰出来了,说明,在前台esayUI中,只能识别固定形式的json串。
通过以上,实现了easyUI分页显⽰数据。实现了基本的查询功能。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论