从头开始学习JAVA 5--Spring MVC
来源:互联网 发布:如何查域名在哪里备案 编辑:程序博客网 时间:2024/05/01 05:27
4 Spring MVC
1)引入Jar
在IVSMDemo中,引入如下jar包:
注意引入Java EE 5 Library
2)配置Web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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">
<!--显示标题 -->
<display-name>xmr_Spring_Demo</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Handles all requests into the application -->
<!--设置Spring配置文件路径 -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/springContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--Spring mvc处理的资源(action) -->
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意:
要在WebRoot\WEB-INF下,添加默认的程序配置文件applicationContext.xml,或者在web.xml中指定。
applicationContext.xml的内容:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
</beans>
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/webservice.xml
</param-value>
</context-param>
</web-app>
3)Spring配置文件
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resourcesmapping="/resources/**"location="/resources/"/>
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scanbase-package="com.ivsm.controller"/>
<!-- Configures the @Controller programming model 配置控制器模块-->
<mvc:annotation-driven/>
<!--把local保存在cookie中 -->
<beanid="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<!--组成视图的路径,保护目录/WEB-INF/views下的.jsp资源 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/views/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
<!--欢迎页面 -->
<mvc:view-controllerpath="/"view-name="index"/>
</beans>
schemaLocation
注意schemaLocation要与jar的版本一致,否则会导致在无网络的情况下,找不到Schem。
组成视图的路径
mvc:resources
context:component-scan
控制器包规则,符合该规则包中的类认为是控制器类(控制器类以@Controller标注)。
4)添加JSP页面
根据组成视图路径的规则,在“WEB-INF/views/”下,创建一个JSP页面(Index.jsp)
。
5)添加控制类
根据配置,添加com.ivsm.controller包下的控制类。
为该类添加“@Controller”标签,表名该类是一个控制类;
添加“@RequestMapping”标签(例如:@RequestMapping(value = "/MyTest/*")),该控制类处理来自/user/下的请求。
添加init函数,来为Index页面进行初始化,返回值为jsp页面的路径(去掉/WEB-INF/views/和.jsp)
@Controller
@RequestMapping(value ="/MyTest/*")
publicclass UserCT {
/**
*初始化页面
*
*@return前台转向
*/
@RequestMapping(value ="default")
public String init(ModelMap mdlMap) {
return"user/Index";
}
}
该函数表示,对Web请求(”/MyTest/default”)映射到(/WEB-INF/views/user/Index. jsp)页面。
类代码:
package com.ivsm.controller.Web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value ="/MyTest/*")
publicclass UserCT {
/**
*初始化道路管理页面
*
*@return前台转向
*/
@RequestMapping(value ="default")
public String init(ModelMap mdlMap) {
return"user/Index";
}
}
6)访问页面
运行IVSMWebDemo,访问http://localhost:8080/IVSMWebDemo/MyTest/default。
注:
我的很多地方并不规范,因为初次接触的缘故,高手告诉我,所有源代码最好放在src/main/java下面,资源放在src/main/resources下面,逻辑代码以*Service命名,数据库接口以*Mapper命名,请求对象以*Vo命名等。
- 从头开始学习JAVA 5--Spring MVC
- 从头学习Spring MVC
- 从头开始学习java
- 从头开始学习Java
- 从头开始学习JAVA
- 从头开始,学习Java Web
- spring mvc+ELK从头开始搭建日志平台
- 从头开始学习JAVA 2--Tomcat
- 从头开始学习JAVA 3--MyEclipse
- 从头开始学习JAVA 6--引入JQuery
- 从头开始学习Java-JavaEE基础
- 从头开始学习MFC~
- 从头开始学习Android
- 今天开始从头学习
- 从头开始学习dojo
- 从头开始学习C
- 从头开始学习
- 从头开始学JAVA
- Linux 命令英文全称
- gcc: error trying to exec 'cc1plus': execvp: 没有该文件或目录
- Android操作HTTP实现与服务器通信
- HTML5笔记(2)概述
- REDO管理
- 从头开始学习JAVA 5--Spring MVC
- WF4.0 入门系列(二) The CodeActivity
- Android中发送Http请求实例(包括文件上传、servlet接收)
- NullPointerException 与 方法参数检查
- Ruby学习笔记3
- 相信自己,不能逃避
- 从头开始学习JAVA 6--引入JQuery
- ubuntu12.04中完美搭建LAMP环境及打开错误提示的方
- iOS应用比Android应用泄露更多个人数据