spring_mvc(一)环境搭建
程序员文章站
2022-07-14 12:32:44
...
1. 建立项目spring_mvc_test
2. 引入jar文件,如图:
3. 下面编辑web.xml文件,配置spring的拦截器:
4. 配置返回结果的视图spring-servlet.xml:
5.现在可以编写controler代码了:
现在在浏览器中输入:http://localhost:8080/spring_mvc_test/simple
然后在浏览器中显示Hello, this is a simple example
2. 引入jar文件,如图:
3. 下面编辑web.xml文件,配置spring的拦截器:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <display-name>Spring3MVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
4. 配置返回结果的视图spring-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd"> <context:component-scan base-package="net.viralpatel.spring3.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
5.现在可以编写controler代码了:
package net.viralpatel.spring3.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping("simple") public @ResponseBody String helloWorld() { String message = "Hello, this is a simple example"; return message; } }
现在在浏览器中输入:http://localhost:8080/spring_mvc_test/simple
然后在浏览器中显示Hello, this is a simple example
上一篇: 并发编程(一)
下一篇: linux查看操作系统信息
推荐阅读
-
如何搭建PhpStorm 10.0.2开发环境?PhpStorm 10.0.2环境搭建安装图文教程
-
Android SDK + Appium 环境搭建
-
CentOS7 LNMP+phpmyadmin环境搭建 第三篇phpmyadmin安装
-
CentOS7 LNMP+phpmyadmin环境搭建 第一篇虚拟机及centos7安装
-
CentOS7 LNMP+phpmyadmin环境搭建 第二篇LNMP环境搭建教程
-
PySpark与GraphFrames的安装与使用环境搭建过程
-
在MAC上搭建python数据分析开发环境
-
VMware中linux环境下oracle安装图文教程(一)
-
脱离脚手架来配置、学习 webpack4.x (一)基础搭建项目
-
Ubuntu 16 Java Develop环境快速搭建