看了网上各种各样的maven创建springMVC环境,但是要么说的不详细,要么不能用。经过长时间的摸索,终于知道怎样搭建环境了。
在eclipse->new->maven
右键项目,maven->update project,弹出下面对话框
点击OK按钮。
可以看到,所有跟springMVC相关的jar包都包含进来了。
下一步创建文件夹
右键项目,选中build path,弹出如下对话框
由于maven自身的原因,创建文件夹失败,我们把这些missing的文件夹remove,然后自己添加上。
点击finish按钮,就可以看到我们需要的文件夹已经创建好了。
按照下图分别创建各个文件
源代码如下:
CheckUserController
---------------------------------------------------------------------------------------------------------------------
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.servlet.ModelAndView;
@Controller
public class CheckUserController {
//private CheckUserServer ckuserserver;
@RequestMapping(value="/checkuser", method = RequestMethod.GET)
public ModelAndView greeting(/*@RequestParam(value="user",required=true) String user,
@RequestParam(value="password",required=true) String pswd*/) {
//List<Object> mm=CheckUserServer.getUserInfos();
//int s = mm.size();
return new ModelAndView("/HelloWorld2");
}
}
HelloWorld2.jsp
---------------------------------------------------------------------------------------------------------------------------------<%@ page language="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
22222222222222222222222222222222222222222222
</body>
</html>
spring-servlet.xml
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.hm.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
项目右键->build project,会发现文件提示出错,httpservlet is not found
项目右键,build path
之后右键项目,build project,可以看到错误提示消失
下面运行一下项目试一下
为了避免之前的server的干扰,先把server删除
右键项目->debug as-> debug onserver
可以看到,成功了!
源代码链接:http://download.csdn.net/detail/chunfengdeyiding/9737304