Eclipse+maven+spring

来源:互联网 发布:织梦cms轮播 编辑:程序博客网 时间:2024/05/21 17:06

1 使用maven构建web项目

1.1 创建maven工程

new--》other--》maven--》Maven Project--》next(勾选Create a simple project)
--》填写GroupID、ArtifactID、version、Packaging(选择jar)

1.2 将工程变为web项目

右击项目名称--》Properties--》Project Facets--》点击Convert  to facetd form--》勾选DynamicWebModule。
结果:项目中多了一个WebContent目录。
将webcontent目录复制到main目录下并改名为webapp,结果如下图

1.3 设置发布规则

右击项目名称--》Properties--》Deployment Assembly(根据自己的习惯修改)

2 添加spring

2.1 导入架包

使用maven后,导入架包非常简单,只需配置好pom.xml项目就会自动从中央仓库中获取架包。
导入架包时需要填写Group Id和artifact id、version,他们的值可以百度,版本号建议去官网查看,使用稳定版。

2.2 web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>  <!-- 默认访问页面 -->  <welcome-file-list>    <welcome-file>hello.jsp</welcome-file>  </welcome-file-list>  <!-- 通过监听器对spring容器进行实例化 -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- spring过滤器 -->  <servlet>  <servlet-name>springMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <load-on-startup>1</load-on-startup><!-- 服务器已开启就启动 -->  </servlet>  <servlet-mapping>  <servlet-name>springMVC</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>  <!-- 设置Spring配置文件的路径路径 -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:springMVC-servlet.xml</param-value>  </context-param></web-app>

2.3 springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.2.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><!-- 扫描 --><context:component-scan base-package="com.yxd"/><!--  --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/View/"/><property name="suffix" value=".jsp"/></bean></beans>

2.4 control

package com.yxd.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.context.annotation.Scope;@Controllerpublic class HelloController {//@RequestMapping表示用那个url来对应@RequestMapping(value="/hello") public String printWelcome(ModelMap model){//model.addAttribute("message","Spring 4 MVC Hello World");System.out.println("hello");return "hello";}/* * spring传值 * 页面传进spring:直接在方法中设置一个参数name,自动获取请求中与name名称相同的url参数, * 如果url没有该参数则name为null * spring返回页面:使用Model对象addAttribute设置键值对 * 页面显示参数${name} */@RequestMapping(value="helloParam")public String helloParam(String name,Model model){System.out.println(name+",helloParam");model.addAttribute("name", name);return "hello";}public void test1(){System.out.println("成功!");}}

2.5 测试

2.5.1 java Application--测试spring bean

package com.yxd.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.ui.Model;import com.yxd.controller.HelloController;public class TestBean {public static void main(String args[]){ApplicationContext ctx =new ClassPathXmlApplicationContext("springMVC-servlet.xml");HelloController h=(HelloController)ctx.getBean("helloController");h.test1();}}

2.5.2 Web

在src/main/webapp/View中新建文件hello.jsp--测试springMVC
访问地址:http://localhost:8080/springDemo/helloParam?name=yy
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="utf-8"%><!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=ISO-8859-1"><title>Insert title here</title></head><body>View/hello, ${name}!!</body></html>


3 架构

src/main/java:源代码
src/main/resources:配置文件


4 源代码



0 0
原创粉丝点击