MyEclipse2015+maven搭建SpringMVC环境

新建一个Maven Project工程

MyEclipse2015+maven搭建SpringMVC环境_第1张图片


直接下一步
MyEclipse2015+maven搭建SpringMVC环境_第2张图片

选择 maven-archetype-webapp
MyEclipse2015+maven搭建SpringMVC环境_第3张图片

输入Group Id和Artifact Id
MyEclipse2015+maven搭建SpringMVC环境_第4张图片

右键 —> Build Path —> Configure Build Path —> 选择Libraries
如果JRE System Library 是低于1.6版本的则,remove掉,重新Add Library添加进来

如果新建项目后,jsp文件报错,是因为没有添加Tomcat运行时相关类导致的,只要Add Library,选择MyEclipse Server Library,再选Tomcat 7.0就行了


新建几个文件夹 src/main/java    src/main/resources    
                          src/test/java      src/test/resources
在MyEclipse其他版本的就可以直接new Source Folder,但是在MyEclipse就只有new Folder
但是不要紧,新建完之后,右键该文件夹,Build Path —> Use as Source Folder

右键 —> Build Path —> Configure Build Path —> 选择Source
能看到4个文件夹,每个都选择 Output folder,Edit,更换编译文件的存放路径
src/main/java 和 src/main/resource 的改为 target/classes
src/test/java 和 src/test/resource 的改为 target/test-classes

MyEclipse2015+maven搭建SpringMVC环境_第5张图片

MyEclipse2015+maven搭建SpringMVC环境_第6张图片

MyEclipse2015+maven搭建SpringMVC环境_第7张图片



右键项目 —> MyEclipse —> Project Facets[Capabilities] —> Manage,勾上spring,这样就为项目安装了spring插件了

MyEclipse2015+maven搭建SpringMVC环境_第8张图片


MyEclipse2015+maven搭建SpringMVC环境_第9张图片


新建一个HelloWorld-servlet.xml,勾上beans、context、mvc

MyEclipse2015+maven搭建SpringMVC环境_第10张图片


MyEclipse2015+maven搭建SpringMVC环境_第11张图片

MyEclipse2015+maven搭建SpringMVC环境_第12张图片


到这里所有的 繁琐的鼠标点击 就结束了,接下来就是配置xml和写代码的事情了

首先配置pom.xml,用maven自动导入SpringMVC所依赖的包
可以在  http://mvnrepository.com/   搜索
在这里要引入 spring-webmvc、jstl、standard 的jar包

pom.xml

  4.0.0
  org.aiyouwei
  web
  war
  0.0.1-SNAPSHOT
  web Maven Webapp
  http://maven.apache.org
  
  
    
      junit
      junit
      3.8.1
      test
    
    
	org.springframework
	spring-webmvc
	4.1.4.RELEASE
	
  	
	javax.servlet
	jstl
	1.2
	
  	
	taglibs
	standard
	1.1.2
	
  
  
  
  
    web
  




web.xml




  	  
        HelloWorld  
        org.springframework.web.servlet.DispatcherServlet  
        1  
      
      
        HelloWorld
        /  
    
    


HelloWorld-servlet.xml




	  
	  
	   
	  
	
	
	  
	  
	    
	      
	      
	
	
	  
	




HelloWorldController.java
package org.aiyouwei.web.controller;
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.Controller;  
public class HelloWorldController implements Controller {  

    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {  
       //1、收集参数、验证参数  
       //2、绑定参数到命令对象  
       //3、将命令对象传入业务对象进行业务处理  
       //4、选择下一个页面  
       ModelAndView mv = new ModelAndView();  
       //添加模型数据 可以是任意的POJO对象  
       mv.addObject("message", "Hello World!");  
       //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面  
       mv.setViewName("hello");
       return mv;  
    }  
}

hello.jsp
注意:一定要加 <%@page isELIgnored="false" %> 否则,JSTL表达式无效
<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%>
<%@page isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    Hello World
  
  
  
    ${message}
  


通过请求:http://localhost:8080/web/hello,如果页面输出“Hello World! ”就表明我们成功了!


你可能感兴趣的:(j2ee,springmvc)