环境搭建
Note:
如果由于网络问题无法连接maven repository, 进而导致无法下载或更新dependencies/plugins, 可以尝试添加proxy. 仍然无效, 请尝试用ip代替host.
<proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <host>proxy.xxx.xxx</host> <port>8080</port> </proxy>
此类错误常见提示:
如果个别jar包下载有问题, 请尝试在local repository中删除对应文件夹下的***.lastUpdated文件, 让Maven尝试重新下载.
如果使用Proxy依旧无法从remote repository下载关联jar包, 有可能是当前的网络供应商无法访问maven的repo. 请使用mirror:
<mirror> <id>ibiblio.org</id> <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url> <mirrorOf>central</mirrorOf> <!-- United States, North Carolina --> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror>
参考文章: maven联通网络下中央仓库不能访问的解决办法
新建Maven Web项目
选择所需的packaging类型为war, 填写group id和artifact id, 按自己的版本习惯填写version, 点击Finish完成创建.
可参考 http://www.cnblogs.com/leiOOlei/p/3361633.html#3099423
遇到的问题:
创建Maven Project后, "src/main/java" (missing)
解决方法: project->property->Java Build Path->Libraries->选择正确的JRE版本
为项目添加Dependencies
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>
Maven自动部署Web项目到Tomcat
<role rolename="admin"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="admin,manager-gui,manager-script"/>
<server> <id>tomcat</id> <username>admin</username> <password>admin</password> </server>
<build> <finalName>SpringMVCDemo</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat</server> </configuration> </plugin> </plugins> </build>
SpringMVC Demo
参考博文: http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html#2842487
1. 配置web.xml
web.xml中ContextLoaderListener和DispatcherServlet的作用:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>demo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>home.html</welcome-file> </welcome-file-list> </web-app>
2. 配置applicationContext.xml
默认情况下, Spring容器的配置文件是WEB-INF/applicationContext.xml.
在HelloWorld中,因暂时不需要Session, Services, DataSource等bean, 所以直接使用最简单的applicationContext.xml如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> </beans>
注1: 如果部署到server发生启动Application失败, 报"UnknownHostException www.springframework.org", 则需要更新bean的dtd.
注2: 如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,则在web.xml文件中可以参考下面代码修改
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml </param-value> </context-param>
注: web.xml中classpath:和classpath*的区别
3. 配置servlet:
默认情况下, 配置文件为/WEB-INF/[your-servlet-name]-servlet.xml. 因为在web.xml中servlet命名为demo, 所以对应配置文件名为demo-servlet.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.demo.springmvc.controller" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/ui/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
4. 在pom.xml中添加Spring Dependencies: spring-webmvc, spring-web.
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency>
测试HelloWorld
1. 创建View:
选择一个自己喜欢的路径, 创建jsp文件, 如: webapp/ui/user/pages/login.jsp
注意: 路径, 以及文件后缀名, 需和demo-servlet.xml中定义的一致.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body> This is the login page. </body> </html>
2. 创建控制器:
利用annotation, Spring会在demo-servlet.xml所配置的base-package中自动扫描带@Controller的控制器
注意: ViewName需和login.jsp保持一致.
package com.demo.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class LoginController { @RequestMapping("/login") public ModelAndView handleLoginRequest() { ModelAndView mv = new ModelAndView(); mv.setViewName("user/pages/login"); // mv.addObject(attributeName, attributeValue); return mv; } }
3. 运行机制简单描述:
根据web.xml的配置, 部署并启动后, 默认访问页面是home.html(放在webapp目录下).
<html> <head> <title>Home</title> </head> <body> <h2>Welcome to Spring MVC by Maven - Hello World!</h2> </body> </html>
当我们在URL上输入login.do时, 根据web.xml的配置, servlet "demo"会得到该request, 并且根据RequestMapping的值, 找到匹配的controller, 也即LoginController, 来进行处理. LoginController则根据ViewName, 找到最终需要展示的view页面, 也即login.jsp. 最终得到结果:
附: 项目结构图