当前位置: 移动技术网 > IT编程>开发语言>Java > 三步轻松搭建springMVC框架

三步轻松搭建springMVC框架

2019年07月19日  | 移动技术网IT编程  | 我要评论
一、搭建步骤 1、导入jar包、创建项目包结构 2、在web.xml中配置前端控制器 3、编写springmvc核心配置文件 4、编写pojo类和controlle

一、搭建步骤

1、导入jar包、创建项目包结构

2、在web.xml中配置前端控制器

3、编写springmvc核心配置文件

4、编写pojo类和controller类测试

二、实现

1、导入jar包、创建项目包结构

 2、在web.xml中配置前端控制器

<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
<!-- 指定springmvc核心配置文件位置
如果没有指定那么默认就会去"/web-inf/+ <servlet-name>标签中内容 + -servlet.xml"中找
例如:"/web-inf/springmvc-servlet.xml"
-->
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

3、编写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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 配置@controller注解扫描 -->
<context:component-scan base-package="cn.it.controller"></context:component-scan>

</beans>

4、编写pojo类和controller类测试

pojo类代码:

package cn.it.pojo;

import java.util.date;

public class items {

private integer id;
private string name;
private float price;
private string pic;
private date createtime;
private string detail;

public integer getid() {
return id;
}

public void setid(integer id) {
this.id = id;
}

public string getname() {
return name;
}

public void setname(string name) {
this.name = name == null ? null : name.trim();
}

public float getprice() {
return price;
}

public void setprice(float price) {
this.price = price;
}

public string getpic() {
return pic;
}

public void setpic(string pic) {
this.pic = pic == null ? null : pic.trim();
}

public date getcreatetime() {
return createtime;
}

public void setcreatetime(date createtime) {
this.createtime = createtime;
}

public string getdetail() {
return detail;
}

public void setdetail(string detail) {
this.detail = detail == null ? null : detail.trim();
}
}

controller类代码:

package cn.it.controller;

import java.util.arraylist;
import java.util.list;

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;

import cn.it.pojo.items;

@controller
public class itemscontroller {

//@requestmapping指定url到请求方法的映射,例如:
@requestmapping("/itemslist")
public modelandview itemslist(){
list<items>itemlist = new arraylist<items>();

//商品列表
items items_1 = new items();
items_1.setname("联想笔记本_3");
items_1.setprice(6000f);
items_1.setdetail("thinkpad t430 联想笔记本电脑!");

items items_2 = new items();
items_2.setname("苹果手机");
items_2.setprice(5000f);
items_2.setdetail("iphone6苹果手机!");

itemlist.add(items_1);
itemlist.add(items_2);

/*
* 模型和视图:
* model模型:模型对象中存放了返回给页面的数据
* view视图:视图对象中指定了返回的页面的位置
*/
//创建modelandview对象
modelandview modelandview = new modelandview();
modelandview.addobject("itemlist", itemlist);
modelandview.setviewname("/web-inf/jsp/itemlist.jsp");
return modelandview;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网