【Spring in action】基于xml配置:在SpringMVC中使用Spring容器
来源:互联网 发布:用c语言编写一个小游戏 编辑:程序博客网 时间:2024/06/06 18:40
前言:
可能作者的层次较高,并没有考虑到SpringMVC+Spring的配置还有新人自学不知道吧。捂脸!
正文:
一、配置整合springmvc+spring:
1、在src也就是classpath下新建spring-serlvet.xml配置文件,用来配置spring容器的bean:扫描model文件夹下所有。
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="model"></context:component-scan> </beans>2、在WEB-INF下配置spring-servlet.xml,配置springmvc
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 配置handlerMapping --><bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前缀 :说明如果直接放在web-inf下,不需要加前缀--><property name="prefix" value="WEB-INF/view/"></property><!-- 后缀 : --><property name="suffix" value=".jsp"></property></bean><!-- 加入注解驱动 --><mvc:annotation-driven></mvc:annotation-driven><!-- 扫描器 --><context:component-scan base-package="com.springmvc.controller"></context:component-scan></beans>3、在WEB-INF下配置web.xml,将spring+springmvc整合起来。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>SpringMVC2</display-name><!-- 配置Spring1:配置spring-servlet.xml路劲 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-servlet.xml</param-value></context-param><!-- 配置Spring2:配置Spring监听2 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 创建分发Servlet --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
二、测试springmvc+spring:
1、在src下新建model文件夹,新建user对象:
package model;import java.io.Serializable;public class User implements Serializable{private int userId;private String name;public User(int i, String string) {this.userId = i;this.name = string;}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User [userId=" + userId + ", name=" + name + "]";}}2、在model文件夹下新建接口:为什么用接口?因为此处可以通过接口实现自动装配。
package model;public interface IUserService {User getUserById(int userId); }3、在model下写一个接口的实现类:此处可写操作数据库的方法等操作或者业务逻辑。
package model;import org.springframework.stereotype.Component;@Component public class UserServiceImpl implements IUserService { @Override public User getUserById(int userId) { return new User(1,"xionghaibo"); } }
4、写一个index.jsp用来网页请求返回展示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>${helloworld}</h1></body></html>5、整合上述所有步骤。在src下新建文件夹com.springmvc.controller,在文件夹下新建controller控制器helloController:
package com.springmvc.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import model.IUserService;@Controllerpublic class HelloController {@AutowiredIUserService IUserService;//返回到index.jsp@RequestMapping(value="/hello.do")public String hello(String userName,Model model){System.out.println(userName);model.addAttribute("helloworld","hello"+IUserService.getUserById(1));return "index";}}6、目录结构一览:
7、测试结果一览:
阅读全文
0 0
- 【Spring in action】基于xml配置:在SpringMVC中使用Spring容器
- 【Spring in action】基于Java配置SpringMVC中使用Spring及SpringMVC各种请求详解
- 【Spring in action】SpringMVC基于xml及java配置的简单运用
- Spring In Action(二):基于XML配置装配bean
- spring in action-最小化spring XML配置
- 在独立的环境中通过使用基于 Java 的配置创建和使用Spring 容器
- 在WEB.xml文件中配置Spring容器
- 【Spring in action】Spring、SpringMVC、Mybatis,Mysql使用详解
- 在struts.xml文件中使用spring代理,管理action
- Spring in Action(五):基于Java Config的SpringMVC
- Spring In Action(四):基于XML的AOP
- web.xml中spring/springmvc配置详解
- Spring in action 03 -- 装配 Bean(xml配置)
- Spring in Action(八):Spring中使用JDBC
- 在Spring IOC容器中配置Bean
- 配置springmvc在其他类中(spring容器外)获取注入bean
- 基于xml配置spring
- Spring+SpringMVC+Hibernate框架完整版配置(基于xml)
- linux 磁盘分区
- 杂志投稿流程
- Win10输入指示器关掉后自动恢复的问题
- OkHttp拦截器
- vue嵌套路由-params传递参数
- 【Spring in action】基于xml配置:在SpringMVC中使用Spring容器
- CentOS怎么安装Taskctl5.0
- NB-IoT模组功耗测试
- javascript中关于锚的作用
- 用js制作论坛发贴
- hbase表的缓存
- 挑战程序竞赛系列(80):4.3 2-SAT(4)
- Linux解压缩
- 山东师范大学-传媒学院首页