搭建 第一个SpringMvc 项目
来源:互联网 发布:飞利浦淘宝授权店 编辑:程序博客网 时间:2024/05/17 03:03
1.加入相关JAR
commons-logging-1.1.1.jar
spring-aop-4.1.4.RELEASE.jar
spring-beans-4.1.4.RELEASE.jar
spring-context-4.1.4.RELEASE.jar
spring-core-4.1.4.RELEASE.jar
spring-expression-4.1.4.RELEASE.jar
spring-tx-4.1.4.RELEASE.jar
spring-web-4.1.4.RELEASE.jar
spring-webmvc-4.1.4.RELEASE.jar
2.编写WEB.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>Springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置DispatcherServlet 一个初始化参数;配置springmvc 的配置文件的位置和名称 --> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下, 名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
SpringMvc 配置文件的文件名 为 web.xml中的[servlet-name]-servlet.xml
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="com.sqc.springmvc" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>4.java代码
package com.sqc.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWord {private final static String SUCCESS="success";/** * 1. 使用 @RequestMapping 注解来映射请求的 URL * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析: * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作 * * /WEB-INF/views/success.jsp * * @return */@RequestMapping("/helloword")public String helloWord(){System.out.println("hello Word");return SUCCESS;}}
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> </head> <body> success page. <br> </body></html>
访问 路径如下 http://127.0.0.1:8080/SpringMvc/helloword
页面展示展示出来 即可
0 0
- 搭建 第一个SpringMvc 项目
- Spring学习笔记<一> 使用SpringMVC搭建第一个项目
- JavaWeb学习之SpringMVC:MAVEN搭建第一个WEB项目
- SSM框架的搭建(Spring+SpringMVC+Mybatis第一个项目的搭建)
- 搭建第一个jfinal项目
- 搭建第一个HelloWorld项目
- spring第一个项目搭建
- springmvc第一个项目helloworld配置
- SpringMVC的第一个项目创建步骤
- SpringMVC 入门------ 第一个SpringMVC框架的JavaWeb项目
- SpringMVC开发——第一个SpringMVC项目
- 用Intellij搭建第一个web项目
- cxf环境搭建与第一个项目
- maven搭建第一个web项目
- 01:搭建第一个springboot项目
- 搭建第一个scrapy项目的常见问题
- SpringMVC环境搭建第一个程序Hello World
- 第一个SpringMVC+Hibernate框架搭建之路
- 在Windows下编译PHP和PHP扩展
- ubuntu遇到的问题
- OpenCV学习笔记二 OpenCV 2.49 + VS2012 配置指南
- 云存储中的数据完整性校验——综述
- ExtJs4.2 登陆界面(点击验证码自动刷新,label实现click事件)
- 搭建 第一个SpringMvc 项目
- 安卓逆向学习笔记(0)- HelloWorld.apk的文件结构
- 蓝桥杯试题 算法训练 安慰奶牛
- Activiti学习入门(1) -- 创建Activiti的23张表
- eXtremeDB JNI 的Connection
- 过滤器、监听器、拦截器的区别
- java核心技术之JDBC (一)
- elipse中配置jadclipe之后无法双击编译
- Servlet和JSP规范及版本对应关系