搭建记录:springmvc + maven
来源:互联网 发布:在端口23连接失败 编辑:程序博客网 时间:2024/06/05 02:19
新建maven项目
选择 maven-archetype-webapp:
填写项目名称:
修改项目配置:
Java Complier 的版本
Project Facets 的版本
同时注意:
Builder Path 中的JDK版本
pom 中builder的JDK版本
添加依赖:
servlet-api
springmvc依赖
添加依赖:
spring-webmvc
修改web.xml
<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> <!-- 自定义 默认是servlet-name-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
关于web.xml配置问题:
Servlet 2.3
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-appPUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"“http://Java.sun.com/dtd/web-app_2_3.dtd"><web-app>…</web-app>
Servlet 2.4
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "version="2.4">…</web-app>
Servlet 2.5
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5">
Servlet 3.0
<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
其中:
Servlet 2.5 版本以后支持多个url映射
<servlet-mapping> <servlet-name>servletName</servlet-name> <url-pattern>/index</url-pattern> <url-pattern>/login</url-pattern></servlet-mapping>
新建和上面servlet名称对应的xml
srping-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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置扫描的包 --> <context:component-scan base-package="com.grp.*" /> <!-- 注册HandlerMapper、HandlerAdapter两个映射类 --> <mvc:annotation-driven /> <!-- 访问静态资源 --> <mvc:default-servlet-handler /> <!-- 处理器映射器 --> <bean name="/0101" class="com.grp.testing.springmvc.controller.TestingController1"></bean> <!-- 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean></beans>
配置路径问题:可在pom的build标签内进行设置(包括其他路径)
<resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
新建类TestingController1
package com.grp.testing.springmvc.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 TestingController1 implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub ModelAndView result = new ModelAndView(); result.addObject("data", "123"); result.setViewName("/page/testing1.jsp"); return result; }}
修改testing1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>springmvc</title></head><body> <h1>This is a springmvc testing1</h1></body></html>
运行结果:
阅读全文
0 0
- 搭建记录:springmvc + maven
- Intellij14搭建Maven的SpringMVC记录
- Maven搭建Mybatis-spring-springMVC项目记录
- Maven搭建SpringMVC+Mybatis项目时遇到的问题记录
- 搭建maven+Spring+Springmvc+mybatis环境出错记录
- Maven 创建SpringMVC记录
- eclipse maven springmvc搭建
- Maven 搭建 SpringMVC
- 搭建maven+springmvc+mybatis
- Springmvc-maven搭建
- springmvc+maven环境搭建
- 使用Maven搭建SpringMVC
- 搭建 maven+springmvc+dubbo
- 使用Maven搭建SpringMVC
- 使用Maven搭建SpringMVC
- Maven搭建SpringMVC+Hibernate
- maven搭建springmvc环境
- 使用Maven搭建SpringMVC
- Dockerfile中ENTRYPOINT 和 CMD的区别以及RUN的作用
- Python利用SMTP发送邮件
- 《剑指offer》笔记-第5章(1)
- ssd目标检测整理
- noip2008T2火柴棒等式题解
- 搭建记录:springmvc + maven
- 整数划分问题
- hdu 1053 huffman编码
- ubuntu下建立PPTP-VPN客户端拨号连接(绝对可用)
- react事件
- 17、oracle:oracle模糊查询,截取字符串以及搜索字符等函数
- Java集合学习小结
- 实现CSS等分布局的5种方式
- Python:zip的妙用