SpringMVC搭建------最简单的实现

最近在学Springmvc 

因为开始学的时候是连接数据库的,实在没搞懂单独的SpringMVC怎么搭建

1,项目结构

2,项目代码

2.1web.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">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 核心控制器 -->
  <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:applicationContext.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

主要说说:
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  	</init-param>
其中<param-value>**.xml</param-value> 
如果不写,则使用默认值:/WEB-INF/<servlet-name>-servlet.xml 
在本文中也就是说 /WEB-INF下创建spring-servlet.xml
spring-servlet.xml取代 applicationContext.xml,就不需要applicationContext.xml

2.2 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:p="http://www.springframework.org/schema/p"
	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-3.1.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	<context:component-scan base-package="com" />
	
	<!-- 启用springmvc注解处理器 -->
	<mvc:annotation-driven />
	<!-- 解决springmvc 不能访问静态资源 -->
	<mvc:default-servlet-handler />
	
	<!-- 配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>


</beans>
这里面就三个东西
<context:component-scan base-package="com" />
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean


2.3book.java

package com.entity;

public class Book {
	private Integer id;
	private String bookName;
	private String author;
	private Integer price;

	public Book(String bookName, String author, Integer price) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
	}

	public Book() {
		super();
	}

	public Book(Integer id, String bookName, String author, Integer price) {
		super();
		this.id = id;
		this.bookName = bookName;
		this.author = author;
		this.price = price;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}
}

2.4BookControl.java
package com.control;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Book;

@Controller
@RequestMapping("/book")
public class BookControl {
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Book book) {
		return "redirect:books";
	}
	
	@RequestMapping("/books")
	public String show(ModelMap model) {
		
		return "show";
	}

}

2.5add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'add.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		
	    <link rel="stylesheet" type="text/css" href="<%=path %>/css/style.css">
	
     <script>
        function check(){
          var regx = /^\d+$/gi;
          var price = document.all.price.value;
          if(!regx.test(price)){
               alert("请输入合理的数据");
               return false;
          }
          return true;
        }
     </script>
	</head>

	<body>
		<center>
		 
			<form action="<%=path%>/book/add" method="post" οnsubmit="return check();">
				名称:
				<input name="bookName" />
				<br />
				作者:
				<input name="author" />
				<br />
				单价:
				<input name="price" />
				<br />
				<Input type="submit" value="添加" />
				<input type="reset" value="取消" />
			</form>
		</center>
	</body>
</html>

最近才学现在也只是搞懂了其中的一个功能!
1,从add.jsp写入的数据被填充到BookControl.java中的add方法的参数book中!
2,add.jsp中表单中input元素的name属性是book.java中的成员属性
3, BookControl.java的“别名”,@RequestMapping("/book")
4,add方法的“别名”,@RequestMapping(value = "/add", method = RequestMethod.POST)
5,因此form表单的 action="<%=path%>/book/add"
6,show 方法的“别名”,@RequestMapping("/books")
注意从add方法调到show方法:return "redirect:books";
show方法最后访问的是show.jsp  直接return “show”;

欢迎广大网友批评指正!






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于SSM框架的网上订餐系统的设计与实现 学生姓名:xxx 专业班级:xxx 学 号:xxx 学 院:xxx 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第1页。 第一章 第二章 第三章 第四章 目录 Contents 系统设计的原则 系统的总体设计 课题研究的背景和意义 系统的功能展示 课题研究的总结 1 2 3 4 5 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第2页。 Part 01 课题研究的背景和意义 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第3页。 单击编辑标题 1.研究背景 近年来,随着互联网技术的快速发展,电子商务的发展也越来越快,人们通过网上操作和沟通来进行商品交易,这大大地方便了人们的生活。在众多的电子商务模式中,网上订餐业务的发展也越来越成熟。随着现代生活节奏的不断加快,人们越来越注重时间的高效利用,而传统的就餐方式往往会浪费人们太多的时间,这时网上订餐这种快捷方便的订餐方式就体现出了它的绝对优势。 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第4页。 人们在网上订餐就不用再跑到餐饮店去就餐了,这样不仅可以节省时间,而且可以在订餐系统上挑选各种口味的美食,这样既给人们提供了方便的饮食方式又能满足人们不同口味的要求,同时对于商家来说通过网上订餐的方式不仅增加了日常的订单数量和整体收入,同时还可以提高商家店铺的知名度。 2.研究意义 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第5页。 Part 02 系统设计的原则 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第6页。 单击编辑标题 系统设计的原则 (1)系统的操作界面一定要简洁、清晰、友好、交互性强,操作步骤 一定要简单灵活,能给用户带来较好的体验感。 (2)系统的功能一定要齐全,而且功能要规范且具有较强的实际操作 性,这样便于增强用户的使用粘性。 (3)考虑到以后业务的发展,系统的设计一定要有较强的可扩展性和 可维护性,这样可以方便以后系统的升级改造。 (4)在保证系统功能完整可靠的条件下,尽可能的降低整体的成本, 在系统的设计和开发中尽量使用比较实用的设备。 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全 共31页,当前为第7页。 Part 03 系统的总体设计 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第8页。 单击编辑标题 1.买家版订餐系统的总体设计 根据现实需要,在买家版订餐系统中设计了以下等功能: 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第9页。 单击编辑标题 2.商家版订餐系统的总体设计 根据现实需要,在商家版订餐系统中设计了以下等功能: 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第10页。 单击编辑标题 3.系统数据库的设计 本订餐系统一共设计了5张表,分别是用户基本信息表、用户收货 地址表、用户订单表、商家基本信息表、商品基本信息表。 1.用户基本信息表 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第11页。 单击编辑标题 3.系统数据库的设计 2.用户收货地址表 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第12页。 单击编辑标题 3.系统数据库的设计 3.用户订单表 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第13页。 单击编辑标题 3.系统数据库的设计 4.商家基本信息表 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第14页。 单击编辑标题 3.系统数据库的设计 5.商品基本信息表 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第15页。 Part 04 系统的功能展示 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第16页。 单击编辑标题 1.买家版订餐系统的功能展示 商家版系统首页(上半部分) 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第17页。 单击编辑标题 1.买家版订餐系统的功能展示 商家版系统首页(下半部分) 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第18页。 单击编辑标题 1.买家版订餐系统的功能展示 搜索功能展示 基于Java-SSM框架的网上订餐系统的设计与实现毕业答辩全文共31页,当前为第19页。 单击编辑标题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值