gradle下的第一个SpringMVC应用
新建gradle project
缺少了很多文件夹和文件,我们自己补充,补充完的目录如下:
HelloController:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping ( "/test" ) public class HelloController { @RequestMapping ( "/hello" ) public ModelAndView del(HttpServletRequest request, HttpServletResponse response) { System.out.println( "----del----" ); return new ModelAndView( "/hello" , "message" , "你好" ); } } |
spring-mvc.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?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: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-4.3.xsd http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- scan the package and the sub package --> <context:component-scan base- package = "controller" /> <!-- don't handle the static resource --> <mvc: default -servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver --> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" id= "internalResourceViewResolver" > <!-- 前缀 --> <property name= "prefix" value= "/WEB-INF/view/" /> <!-- 后缀 --> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?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" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" > <!--START 设置字符编码过滤器--> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class > <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF- 8 </param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--END 设置字符编码过滤器--> <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> <!-- <load-on-startup> 1 </load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout> 20 </session-timeout> </session-config> </web-app> |
index.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%-- Created by IntelliJ IDEA. User: sawyer Date: 2018 / 4 / 19 Time: 下午 11 : 53 To change this template use File | Settings | File Templates. --%> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>我的主页</title> </head> <body> 你好,世界 </body> </html> |
hello.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%-- Created by IntelliJ IDEA. User: sawyer Date: 2018 / 4 / 20 Time: 上午 12 : 08 To change this template use File | Settings | File Templates. --%> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body>${message}helloWorld </body> </html> |
验证效果:
作者: lost blog
出处: http://www.cnblogs.com/JAYIT/
关于作者:专注服务器端开发
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题, 可邮件(sawyershaw@qq.com)咨询.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步