SpringMVC in IDEA开发实践

摘要:
本机本来装了IDEA和Maven。参考以下这篇https://my.oschina.net/gaussik/blog/385697《使用IntelliJIDEA开发SpringMVC网站(一)开发环境》其中初始化项目的时候非常慢,需要参考以下这篇来进行:http://www.cnblogs.com/beiyeren/p/4566485.html《mavengeneratingprojectinbatchmodehang》另外因为maven本来的repository太慢了,所以使用百度内部的repository。然后进入第三个页面:https://my.oschina.net/gaussik/blog/513444按照上文中的配置后,能够继续运行成功。然后进入第四个页面:https://my.oschina.net/gaussik/blog/513614按照以上这篇文章的配置和代码,可以完成blog系统管理端基本的增删改查功能。

按照上篇装过Tomcat之后。

本机本来装了IDEA和Maven。

参考以下这篇

https://my.oschina.net/gaussik/blog/385697

《使用IntelliJ IDEA开发SpringMVC网站(一)开发环境》

其中初始化项目的时候非常慢,需要参考以下这篇来进行:

http://www.cnblogs.com/beiyeren/p/4566485.html

maven generating project in batch mode hang

另外因为maven本来的repository太慢了,所以使用百度内部的repository。在pom.xml最后加的配置如下:

    <repositories>
        <repository>
            <id>nexus-public</id>
            <url>http://maven.scm.baidu.com:8081/nexus/content/groups/public</url>
        </repository>
        <repository>
            <id>nexus-public-snapshots</id>
            <url>http://maven.scm.baidu.com:8081/nexus/content/groups/public-snapshots</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus-public</id>
            <url>http://maven.scm.baidu.com:8081/nexus/content/groups/public</url>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-public-snapshots</id>
            <url>http://maven.scm.baidu.com:8081/nexus/content/groups/public-snapshots</url>
        </pluginRepository>
    </pluginRepositories>
    <distributionManagement>
        <repository>
            <id>Baidu_Local</id>
            <name>Baidu local maven server</name>
            <url>http://maven.scm.baidu.com:8081/nexus/content/repositories/Baidu_Local</url>
        </repository>
        <snapshotRepository>
            <id>Baidu_Local_Snapshots</id>
            <name>Baidu local maven server for snapshots</name>
            <url>http://maven.scm.baidu.com:8081/nexus/content/repositories/Baidu_Local_Snapshots</url>
        </snapshotRepository>
    </distributionManagement>

按照第一份页面改好之后,进入第二个页面:

https://my.oschina.net/gaussik/blog/513353

按照页面进行修改和配置之后,run程序出现以下错误:

无法找到元素 'context:component-scan' 的声明

搜索后采用http://www.ithao123.cn/content-10390816.html的方法加下以下:

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd"

再跑,出现如下错误:

无法找到元素 'mvc:default-servlet-handler' 的声明

根据上一个错误的经验,看了下别人的servlet.xml配置,加上了以下配置:

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd

再次运行,就成功获得了想要的页面:

这里是SpringMVC Demo首页
出现此页面,说明配置成功。

然后进入第三个页面:

https://my.oschina.net/gaussik/blog/513444

按照上文中的配置后,能够继续运行成功。

然后进入第四个页面:

https://my.oschina.net/gaussik/blog/513614

按照以上这篇文章的配置和代码,可以完成blog系统管理端基本的增删改查功能。

然后进入第五个页面:

https://my.oschina.net/gaussik/blog/640164

注意,在新写Controller的时候,都要加上注解。比如@Crontroller.

注意:在添加新blog文章的时候,报错:”The request sent by the client was syntactically incorrect.“

搜索之后,发现是前端字段和后端处理的字段不匹配。将添加文章的addP操作从post改成get,看到了url里面时间是2016-02-01这样的string格式,所以需要在Java里面增加从String到Date的转换。

在网上搜索之后,在BlogEntity的时间前面加上注解:

@DateTimeFormat( pattern = "yyyy-MM-dd" )
private Date pubDate;

文件头部加上引用:

import org.springframework.format.annotation.DateTimeFormat;

依赖关系pom.xml处加上对joda的依赖引用,即可:

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>

通过对get方式的验证,正确了。再改回post方式。

最后,看一下部署的方式:

在Idea IDE项目的target目录下面,有一个SpringMVCDemo的目录。这个目录就是生成好的项目。把这个目录放到Tomcat的webapps目录下,然后重启Tomcat,就可以通过http://localhost:8080/SpringMVCDemo 来访问这个项目了。

但是实际部署之后发现,部署到Tomcat目录后,新启动Tomcat后,只有SpringMVCDemo ,SpringMVCDemo/blogs , SpringMVCDemo/admin/users等输入的url能够work。在JSP代码里面进行redirect的url貌似不可以运行成功,比如会跳到如下:

http://localhost:8080/admin/blogs/show/1

搜索了一些资料,开始是在JSP文件head中加入如下代码:

<%
    String path =request.getContextPath();
    String basePath = request.getScheme()+"://" +request.getServerName()+":" +request.getServerPort()+path+"/";
    %>
    <base href="http://t.zoukankan.com/<%=basePath%>" >

测试之后,发现并没有用。上面这个应该是针对JSP本身做的。

然后看到JSP中的url跳转其实是写在html里面的,所以做了如下修改(注意,. 代表的当前目录指的是/admin/):

<a href="http://t.zoukankan.com/admin/blogs/show/${blog.id}" type="button" class="btn btn-sm btn-success">详情</a>
<a href="http://t.zoukankan.com/admin/blogs/update/${blog.id}" type="button" class="btn btn-sm btn-warning">修改</a>
<a href="http://t.zoukankan.com/admin/blogs/delete/${blog.id}" type="button" class="btn btn-sm btn-danger">删除</a>
改为
<a href="http://t.zoukankan.com/blogs/show/${blog.id}" type="button" class="btn btn-sm btn-success">详情</a>
<a href="http://t.zoukankan.com/blogs/update/${blog.id}" type="button" class="btn btn-sm btn-warning">修改</a>
<a href="http://t.zoukankan.com/blogs/delete/${blog.id}" type="button" class="btn btn-sm btn-danger">删除</a>

还有,注意把 add user/blog里面的/admin/users或者/admin/blogs地址都要改成 ./ ,因为这时候. 已经到了/admin的下一层目录。

另外, update user/blog里面的 updateP地址要改成 ../updateP,因为当前目录已经到了 /admin/user/update。

在测试的时候,发现IDEA没有运行的时候,target包不会更新的,所以需要先在IDEA make 一下或者运行一下,再把target目录下的包放到web-apps里面才能生效。

以上,就是springmvc demo程序的全部内容。当然了,多了解一些前端会有更多好处。所以有时间可以看一下Bootstrap:

http://v3.bootcss.com/

以上全部代码,在oschina原文作者的github里面可以获取:https://github.com/gaussic/SpringMVCDemo

免责声明:文章转载自《SpringMVC in IDEA开发实践》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇c++学习(四):函数分发饼干下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

canvas 动画库 CreateJs 之 EaselJS(下篇)

本文来自网易云社区 作者:田亚楠 继承 对应原文:Inheritance 我们可以继承已有的「显示对象」,创建新的自定义类。实现方法有很多种,下面介绍其中之一。 举例:实现一个继承于 Container 类的自定义类 Button: 共分 4 步: 自定义构造器 继承父类,获得父类的功能 重写已有方法,扩展自身方法 promote 继承来的方法,返回自定...

Spring AOP之使用注解创建切面

上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象。那么现在就让我们使用AspectJ注解来定义切面吧。 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.aop.service.aop; import org.aspectj.lang.ProceedingJoi...

最新的Aliyun Maven仓库地址

一、官网 官网地址:https://maven.aliyun.com 二、配置仓库 打开Maven安装目录下的/conf/settings.xml文件,在mirrors节点下添加: <mirror> <id>nexus-aliyun</id> <mir...

Spring知识点整理---(IOC,DI)

1.Spring的基本应用 1.1Spring概述 1.1.1什么是Spring Spring是由Rod Johnson组织和开发的一个分层的Java SE/EE full-stack(一站式)轻量级开源框架,它以IoC(Inversion of Control,控制反转)和 AOP(Aspect OrientedProgramming,面向切面编程)为内...

讨论研究普通的网页登录按钮的状态

     就在我刚才看一个技术视频的时候,里面提到了一个包括我在内的被很多程序开发人员忽视的问题。这个问题不管是在软件开发工作了几年的技术丰富的研发人员还是刚刚入行的菜鸟往往忽视掉,那就是在网页的登录功能的登录按钮有几种状态呢?分别表示怎样的功能含义呢?下来我们就来说明一下。 1、现在在每个软件开发项目和产品中都会有用户登录的界面,不管是系统或者网站都会有...

NGUI系列教程二

接下来我们创建一个Label,NGUI->Open the Widget Wizard,打开widgetTool对话框,在Template中选择Label,确定AddTo右侧选项为panel,点击,Add To完成Label创建。 1.Label属性窗口如图 1.下面的节奏可能有点快,大家可以先熟悉一下上面讲的流程,再进行下面的学习。接下来我们...