IDEA springmvc 开始项目
开始配置
-- pom.xml --
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>cn.wxy</groupId>
<artifactId>WXYProj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>WXYProj Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.1.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 添加Spring支持 -->
<!-- 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- pring IOC的基础实现,包含访问配置文件、创建和管理bean等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>WXYProj</finalName>
...
</build>
</project>
-- WEB-INF/web.xml --
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
-- WEB-INF/springmvc-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean name="/hello" class="cn.weizhen.HelloController" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>
</beans>
这里指定的BeanNameUrlHandlerMapping, SimpleControllerHandlerAdapter, InternalResourceViewResolver
分别是映射器(处理url->class),适配器(处理class中的方法)和jsp页面调用。这三个bean也是默认的,可以省略不写。
在IDEA中使用javax需要把tomcat中的servlet-api.jar, jsp-api.jsp
加入到WEB-INF/lib下,如果没有lib,可以新建一个,加入这两个jar包后要右键add as Library.
-- src/main/java/cn.weizhen/HelloController.java --
package cn.weizhen;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller {
public static Log logger = LogFactory.getLog(HelloController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("Did called handleRequest");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("WEB-INF/content/welcome.jsp");
modelAndView.addObject("message", "Hello world");
return modelAndView;
}
}
-- WEB-INF/content/welcome.jsp --
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello</h1>
<!-- modelAndView.addObject("message", "Hello world"); -->
${requestScope.message}
</body>
</html>
运行:浏览器键入:http://localhost:8080/hello
结果:
Hello
Hello world
上面的示例中,welcome.jsp使用了EL表达式,因此在pom.xml中导入了jstl的架包。而HelloController实现spring的Controller接口并实现其中的方法:ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
, 这是Spring2.5之前的方式,这种方式只能处理一个单一请求。而Spring2.5之后一般通过基于注解的方式处理,不需要实现任何接口,并且可以支持同时
处理多个请求动作。
使用注解
现在基本上都是使用注解的方式了。
修改springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc">
<context:component-scan base-package="cn.weizhen" />
<beans>
<!--spring可以自动扫描base-pack下面的包或子包下面的Java文件-->
<context:component-scan base-package="cn.weizhen" />
<!--映射器:配置annotation类型的处理映射器-->
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!--适配器:配置annotation类型的处理器适配器-->
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<!--解析器:配置annotation类型的视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<!--由于使用了注解类型,因此不需要再在配置文件中使用XML描述Bean,Spring使用扫描机制查找应用程序中所有基于注解的控制器类。base-pack指定了spring扫描的包-->
<!--<bean name="/hello" class="cn.weizhen.HelloController" />-->
</beans>
</beans>
上面的映射器、适配器、解析器只是为了显示配置,可省略。
修改HelloController.java:
package cn.weizhen;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
public static Log logger = LogFactory.getLog(HelloController.class);
public HelloController() {
}
@RequestMapping({"/hello"})
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.info("Did called handleRequest");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("WEB-INF/content/welcome.jsp");
modelAndView.addObject("message", "Hello world");
return modelAndView;
}
}