Spring4-快速入门
来源:互联网 发布:useragent windows nt 编辑:程序博客网 时间:2024/04/30 01:22
Spring4-快速入门之搭建简单的web项目
1.配置web.xml文件,在web服务器启动时自动启动spring容器,配置Spring MVC框架
<?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"> <!-- 加载指定路径下的spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:smart-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置spring mvc的主控servlet --> <servlet> <servlet-name>smart</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!-- spring mvc处理的url --> <servlet-mapping> <servlet-name>smart</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
smart-context.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.test.spring4"></context:component-scan></beans>
使用Spring MVC也需要配置spring配置文件,默认文件名为servlet名-servlet.xml,例如此处为:smart-servlet.xml,将这个配置文件放到/WEB-INF目录下,具体内容如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 指定controller类的路径 --> <context:component-scan base-package="com.test.spring4.web"></context:component-scan> <!-- 配置视图解析器,讲ModelAndView及字符串解析为具体的页面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property><!-- 指定视图名前添加的前缀 --> <property name="suffix" value=".jsp"></property><!-- 指定视图名后添加的后缀 --> <!-- 例如视图名为success,则会访问/WEB-INF/jsp/success.jsp页面 --> </bean>
2.编写Controller
@Controllerpublic class TestController { @RequestMapping(value="/test.do") public ModelAndView test(String username,String password) { System.out.println("username:"+username); System.out.println("password:"+password); ModelAndView mv = new ModelAndView(); mv.addObject("username", username); mv.setViewName("success"); return mv; }}
这里将TestController类通过@Controller注解声明为一个Controller,@RequestMapping设置访问的url,根据返回的视图,会跳转到/WEB-INF/jsp/success.jsp页面
3.启动web服务,访问http://locahost:8080/spring4/test.do?username=user&password=123456,执行效果如下。
阅读全文
0 0
- Spring4-快速入门
- Spring4快速入门第一章HelloWorld
- Spring4快速入门第五章操作数据库
- Spring4快速入门第六章AOP
- Spring4快速入门第七章事务管理
- Spring4-快速入门之Spring Boot
- Spring4.3入门 快速上手Spring
- spring4入门
- Spring4快速入门第二章配置bean(上)
- Spring4快速入门第三章配置bean(下)
- Spring4快速入门第四章注解装配bean属性
- spring4 mvc 快速入门 - spring boot or not?
- Spring4-快速入门之在IOC容器中装配Bean
- Spring4入门笔记
- spring4入门篇(一)
- Spring4入门小坑
- Spring4.3入门 Spel
- 2.3 Spring4 快速入门-bean 的注入与初始化和销毁
- 韩媒确认LG G6不会登陆中国,LG手机以后不在中国卖了?
- 俞永福宣布阿里大文娱初步整合结果,合一集团并入阿里影业
- 欢迎使用CSDN-markdown编辑器
- ECMAScript6语法使用
- 我的电路实践
- Spring4-快速入门
- c++NULL指针
- 上海约谈共享单车企业:即日起暂停投放
- git新建,更新代码和Git错误non-fast-forward解决
- leetcode 33. Search in Rotated Sorted Array
- Mysql总结1-mysql安装
- 外媒:谷歌风投前CEO创办1亿美元基金,重点关注医疗健康领域
- 据报《黑客帝国》要“重装上阵”了
- 明明只是个造吸尘器的,为何戴森还要在机器人和 AI 上下大力气?