spring中实现springMVC
来源:互联网 发布:实用的app软件 编辑:程序博客网 时间:2024/05/17 08:16
1. applicationContext.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:tx="http://www.springframework.org/schema/tx" 注意:xmlns需要和 xsi 一 一 对应
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<aop:aspectj-autoproxy/> <!-- 要添加本行 -->
<!-- 自动扫描 -->
<context:component-scan base-package="com.yuan"/>
</beans>
2. spring-servlet.xml (mvc配置)
<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-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 加入注解和验证的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 扫描包实现注解的自动装配 -->
<context:component-scan base-package="com.yuan.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3. web.xml
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. controller
package com.yuan.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yuan.bean.Student;
import com.yuan.service.impl.StudentServiceImpl;
//控制器
@Controller
public class LonginController {
@Autowired
private StudentServiceImpl studentServiceImpl;
@Autowired
private Student s;
@RequestMapping()
public String login(){
return "index";
}
@RequestMapping("/show")
public void showStudent(){
System.out.println("show...");
studentServiceImpl.showStudentName();
}
}
5.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="/test_spring/show.do" method="post">
用户名:<input type="text" name="name"/><br/>
密码:<input type="password" name="passWord"><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
6.注意jar包必须放在 lib 下面 , bean ,dao service 配置上对应的注解就ok了。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
主要实现,启动web服务器时,自动装配ApplicationContext 。 装配之后就可以通过 autowried注解活得bean , 不然需要 ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml"); ac.getBean(" ");来获取。
- spring中实现springMVC
- spring+springmvc实现websoket1
- spring+springmvc实现websoket2
- Spring-SpringMVC实现RESTful架构
- spring springmvc实现文件上传
- Spring+Springmvc实现文件上传
- Spring MVC 中 HandlerInterceptorAdapter的使用[SpringMVC中请求拦截器的实现]
- SpringMVC+Spring+mybatis+redis项目从零开始--Springmvc配置实现
- SpringMVC+Spring+mybatis+redis项目从零开始--Springmvc配置实现
- spring + springmvc + cxf 实现webservice restful
- Maven+Mybatis+Spring+SpringMVC实现分页
- springmvc+spring+myibatis 实现ztree 无限菜单
- spring+SpringMVC+Maven+Mybatis+Mysql分页实现
- SpringMVC Spring Mybatis Maven 实现登录功能
- spring springmvc shiro 实现单点登录demo
- 【Spring 核心】(3):SpringMVC 的实现
- spring+springmvc+hibernate,实现分页功能
- spring+springmvc+hibernate,实现分页功能
- c++进阶---IO类详解(二)--文件流的详解
- 分治法
- RMQ学习笔记
- 折叠式的菜单
- nyoj 211&&poj 3660
- spring中实现springMVC
- HDU 2602 (0-1背包)
- 基于稀疏表和线段树的区间最值查询(RMQ)问题
- Android Things专题3 开发环境搭建
- Vue.js基础指令、基本介绍
- POJ 1113 Wall
- URL&HTTP协议
- 一个程序看懂C++ switch 执行流程
- LeetCode 116. Populating Next Right Pointers in Each Node