您的位置:首页 > 编程语言 > Java开发

SpringMVC初探

2016-04-26 15:20 267 查看
一、SpringMVC  helloworld

1、下载

Spring官网改版后找了好久都没有找到直接下载Jar包的链接,下面汇总些网上提供的方法,亲测可用.

1).直接输入地址,改相应版本即可:
http://repo.springsource.org/libs-release-local/org/springframework/spring/4.2.5.RELEASE/spring-framework-4.2.5.RELEASE-dist.zip
2).在1的方法上输入前面部分,有个树形结构可供选择:
http://repo.springsource.org/libs-release-local/org/springframework/spring/
2、导入相关jar包

spring-aop-4.2.5.RELEASE.jar

spring-aspects-4.2.5.RELEASE.jar

spring-beans-4.2.5.RELEASE.jar

spring-context-4.2.5.RELEASE.jar

spring-core-4.2.5.RELEASE.jar

spring-expression-4.2.5.RELEASE.jar

spring-jdbc-4.2.5.RELEASE.jar

spring-orm-4.2.5.RELEASE.jar

spring-test-4.2.5.RELEASE.jar

spring-tx-4.2.5.RELEASE.jar

spring-web-4.2.5.RELEASE.jar

spring-webmvc-4.2.5.RELEASE.jar

另外还需从Struts导入commons-logging-1.2.jar包

3、配置web.xml

Spring官方文档:

<web-app>

    <servlet>

        <servlet-name>example</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>example</servlet-name>

        <url-pattern>/example/*</url-pattern>

    </servlet-mapping>

</web-app>

实际配置:

<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

4、配置SpringMVC配置文件

由于web.xml配置了SpringMVC的配置文件为classpath:springMVC.xml,因此在根目录下新建一个springMVC.xml文件。

Spring官方文档:

<?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">
    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>

    <!-- ... -->

</beans>

实际配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置渲染器 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 结果视图的前缀 -->
<property name="prefix" value="/"></property>
<!-- 结果视图的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.giot.fusion"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<mvc:annotation-driven />

</beans>

参数说明:

 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签

1.<context:include-filter>

2.<context:exclude-filter>

在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写<context:component-scan base-package="com.giot.fusion"/> Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。

当与hibernate集成时容易造成混乱或错误,所以此处设为false,并使用<context:include-filter>进行指定,制定SpringMVC指解析带有@ Controller和@ ControllerAdvice的注解。
<context:component-scan base-package="com.giot.fusion"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

5、controller类中加入相应的注解

@Controller

@RequestMapping("/product/type")

public class CategoryAction {

}

二、SpringMVC对提交数据的处理

1.提交的域名称和处理方法的参数名一致即可

提交的数据:
http://localhost:8080/fusion/product/type/delete.do?id=402881e753eebafe0153eebd39260001
处理方法:

public String deleteCategory(String id) {
boolean result = categoryService.deleteCategory(id);
JSONObject jsonObject = new JSONObject();
if (result) {
jsonObject.put("code", 0);
jsonObject.put("msg", "");
} else {
jsonObject.put("code", -2);
jsonObject.put("msg", "delete category failed");
}
return jsonObject.toString();
}

2. 提交的域名称和处理方法的参数名不一致

提交的数据:
http://localhost:8080/fusion/product/type/delete.do?id=402881e753eebafe0153eebd39260001
处理方法:

public String deleteCategory(@RequestParam("categoryId") String id) {
boolean result = categoryService.deleteCategory(id);
JSONObject jsonObject = new JSONObject();
if (result) {
jsonObject.put("code", 0);
jsonObject.put("msg", "");
} else {
jsonObject.put("code", -2);
jsonObject.put("msg", "delete category failed");
}
return jsonObject.toString();
}

3.提交的是一个对象

要求提交的域名称和对象的属性名一致,处理方法的参数可直接使用对象

提交的数据:
http://localhost:8080/fusion/product/type/add.do?name=lucky&sex=famale
实体类:

public class User {
private String id;
private String name; 
private String sex;

}

处理方法:

public String addUser(User user) {
return user.toString();
}

二、SpringMVC将数据显示到UI

1.使用ModelAndView——需要配置视图解析器

2.通过ModelMap实现——不需要视图解析器
ModelMap需要作为处理方法的参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringMVC