Pom文件的配置:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.coinidea
web
war
1.0-SNAPSHOT
hive.web Maven Webapp
http://maven.apache.org
4.2.5.RELEASE
junit
junit
3.8.1
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
web
people.apache.snapshots
http://repository.apache.org/content/groups/snapshots-group/
false
true
本地安装tomcat,如果不知道tomcat的安装方式,请自己搜索一下。
Tomcat的配置
1.Run->Edit Configurations,点击”+”,选择tomcat local。
2. “Server” Tab, Configure Tomcat的本地路径;”Deployment”选择”+”->Artifact。
3. 运行起来之后,会弹出默认的hello world.
4. 新建自己的controller
5. web.xml的配置
6.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
</web-app>
值得注意的是,上述的servlet-mapping的default配置解决了spring静态文件404的问题。
其他所有的url都会通过dispatcher的配置进行url mapping。
7. dispatcher-servlet.xml的配置
8.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
指定了jsp的view文件夹在/WEB-INF/views/以及运行使用spring的注解功能。所有的controller都会去pachage com.coinidea.controller中寻找。
9. FirstHello controller
package com.cmri.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/first")
public class TestController {
@RequestMapping("hello")
public ModelAndView view() {
ModelAndView mv = new ModelAndView();
mv.addObject("message", "HelloWorld!");
mv.setViewName("hello");
return mv;
}
}
hello.jsp在views文件夹中:
hello world${message}
当浏览器中访问http://localhost:8080/first/hello会显示:
10. Java web Api controller
当需要响应ajax的请求的时候,函数需要加上注解:
@RequestMapping("ajax")
@ResponseBody
public String ajax() {
return " Hello world "
}