Spring MVC 入门示例
程序员文章站
2022-04-14 14:31:36
...
为了简单,将spring-framework中dist下的所有jar包拷贝到项目的WEB-INF/lib目录下
需要添加Apache commons logging日志,此处使用的是commons.logging
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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>30</session-timeout> </session-config> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
以上配置已经增加了由spring提供的编码过滤器来解决乱码问题
编写MVC控制配置文件mvc-config.xml,该文件在WEB-INF的spring文件下,而web.xml中已经配置加载该文件夹下的所有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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀) 比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp” --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean name="/hello.do" class="test.HelloWorldController" /> </beans>
编写控制的类,该类接受参数,设置该参数为视图数据
package test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; // http://localhost:8080/spring/hello.do?user=java public class HelloWorldController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView(); // 添加模型数据 可以是任意的POJO对象 mv.addObject("user", request.getParameter("user")); // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("hello"); return mv; } }
视图使用JSP,页面很简单。在WebRoot下新建文件夹jsp,新建jsp命名为hello.jsp。
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>SpringMVC</title> </head> <body> 您好,${user }! </body> </html>
访问路径http://localhost:8080/spring/hello.do?user=java看效果
请您到ITEYE网站看 java小强 原创,谢谢!
http://cuisuqiang.iteye.com/ !
自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!
推荐阅读
-
hadoop入门之统计单词在文件中出现的个数示例
-
Spring MVC深入学习之启动初始化过程
-
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
-
使用Spring Boot集成FastDFS的示例代码
-
spring security4 添加验证码的示例代码
-
Spring整合Quartz分布式调度的示例代码
-
Spring Boot与Spark、Cassandra系统集成开发示例
-
spring boot aop 记录方法执行时间代码示例
-
spring cloud 的监控turbine-rabbitmq的示例
-
Spring Cloud之服务监控turbine的示例