Spring is one of the most popular and used java frameworks. In this post we show how to create first project in Spring MVC + Maven and how to run it using IntelliJ IDEA and Tomcat!
To create our first project in Spring MVC we need:
JDK 7
Maven
Spring MVC 4.0
Tomcat
IntelliJ IDEA
And of course some basic knowledge about Java and Spring framework.
Firstly I would like to introduce you Spring MVC documentation – I think it’s must-read for all new developers in Spring MVC.
Project structure After that, we can create maven web applicatoin. We can do it in IDEA (File->New Project->Maven module->maven-archetype-webapp) or using Maven:
As as result, we should get following project structure:
?
1
2
3
4
5
6
7
8
|src
|--main
|----resources
|----webapp
|------index.jsp
|------WEB-INF
|--------web.xml
|pom.xml
I also create java directory inside main and I keep there all my java classes. In the default, all .jsp views are directly inside webapp but I keep my views in WEB-INF/views directory – I think it’s more elegant.
Now we can required dependencies to pom.xml file:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
org.springframework
spring-core
4.0.0.RELEASE
org.springframework
spring-beans
4.0.0.RELEASE
org.springframework
spring-context
4.0.0.RELEASE
org.springframework
spring-web
4.0.0.RELEASE
org.springframework
spring-webmvc
4.0.0.RELEASE
Spring configuraition Ok, now we need to configure our application to use Spring MVC. To do this we need to edit our web.xml file and add following:
(web.xml file is available on github, link on the end of this post).
OK, we have web.xml file so we need to create mvc-dispatcher-servlet.xml file inside WEB-INF directory. In this file we can configure our servlet and define spring beans. Our sample file looks like:
In this file we configure base-package which is scan to use annotations. You can change it for your package. Inside this file we can also configure view resolver – I use InternalResourceViewResolver which use .jsp files which are placed in WEB-INF/views/ directory (you can change it as well).
Controller and view OK, we have configured project so now we can create controller which will handle main request. So we createIndexController:
Regarding above code: @Controller annotation makes this class a controller which can handle HTTP requests. Inside this class we created index() method to handle “/” url path (using @RequestMapping annotation). As you can see, this method returns ModelAndView object which is combination of objects-map and view name. As a argument it takes view name (so for us it’s index/index and it points to WEB-INF/views/index/index.jsp because we configured .jsp suffix and WEB-INF/views/ prefix). We’ve added msg object to objects-map and then we can use it in our view.
In /WEB-INF/views/index/ we create index.jsp view file:
?
1
2
3
4
5
6
7
Hello World!
${msg}
And as you can see we used ${msg} variable, which is set in Controller, to print text.
Overall Our project structure now looks like:
This project is available on our GitHub: Spring MVC and Maven basic template
Running project in IntelliJ IDEA 12 OK now we need to configure our Tomcat in IDEA. To do this we need go to Run/Debug Configuration. Then we need to add new configuration and select Tomcat -> Local. As startup page we can set: http://localhost:8080/hello/. Then we have to go to Deployment tab and add artifact: war exploded and application context for this artefact set to the same path as in startup page, so: /hello. Now only save this configuration and you can run project. As a result you should be able to openhttp://localhost:8080/hello/ and see your first Spring MVC webpage:
然后在eclipse里导入maven项目就可以运行了;但是细心的同学会发现,这个工程的propertis中的dynamic web module的version是2.3,java的版本也是1.3,这开玩笑吗?
不是,是真的这样!
那我们如何把它改成3.1,把java 1.3改成1.8;
java的版本修改,直接改就可以了;
但改dynamic web module的版本,要费点功夫:
下面给出的方案,是我读了两个外国人的解决方案后得出的最佳方案:
Open web.xml from project structure http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> Servlet 3.0 Web Application
and update web.xml file for example change version = "2.5"
Also change the org.eclipse.wst.common.project.facet.core.xml file from your project .settings file Follow the steps 1. Window > Show View > Other > General > Navigator
There is a .settings folder under your project directory
Change the dynamic web module version in this line to 2.5 4.Change the Java version in this line to 1.5 or higher
Now refresh your project and get set to run on your server.
Do follow the blog for the solution http://scrapillars.blogspot.in/2014/02/how-to-change-project-facet-in-eclipse.html
The solution is produced with image illustrations
如果出现下面错误:
org.eclipse.core.runtime.CoreException: One or more constraints have not been satisfied
org.springframework.web.context.ContextLoaderListener
contextConfigLocationclasspath*:mvc-dispatcher-servlet.xmlArchetype Created Web Applicationmvc-dispatcherorg.springframework.web.servlet.DispatcherServlet1mvc-dispatcher/
最近mysql数据库经常死掉,用命令net stop mysql命令也无法停掉,关闭Tomcat的时候,出现Waiting for N instance(s) to be deallocated 信息。查了下,大概就是程序没有对数据库连接释放,导致Connection泄露了。因为用的是开元集成的平台,内部程序也不可能一下子给改掉的,就验证一下咯。启动Tomcat,用户登录系统,用netstat -
var a=document.getElementsByClassName('textinput');
var b=[];
for(var m=0;m<a.length;m++){
if(a[m].getAttribute('placeholder')!=null)
b.push(a[m])
}
var l
错误信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cartService': Scope 'session' is not active for the current thread; consider defining a scoped
这个开源软件包是国内的一位高手自行研制开发的,正如他所说的一样,我觉得它可以使一个工作流引擎上一个台阶。。。。。。欢迎大家使用,并提出意见和建议。。。
----------转帖---------------------------------------------------
IK Expression是一个开源的(OpenSource),可扩展的(Extensible),基于java语言
1.在thingking in java 的第四版第六章中明确的说了,子类对象中封装了父类对象,
2."When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the sam
http://www.sap.com/corporate-en/press.epx?PressID=14787
有机会研究下EIM家族的两个新产品~~~~
New features of the 4.0 releases of BI and EIM solutions include:
Real-time in-memory computing –
结构
继承关系
public final class Manifest extends Objectjava.lang.Objectandroid.Manifest
内部类
class Manifest.permission权限
class Manifest.permission_group权限组
构造函数
public Manifest () 详细 androi
关键字:Oracle实现类split函数的方
项目里需要保存结构数据,批量传到后他进行保存,为了减小数据量,子集拼装的格式,使用存储过程进行保存。保存的过程中需要对数据解析。但是oracle没有Java中split类似的函数。从网上找了一个,也补全了一下。
CREATE OR REPLACE TYPE t_split_100 IS TABLE OF VARCHAR2(100);
cr