发表于: 2017-07-22 22:01:59
2 1083
今天做的事:
今天师兄给讲了一下Controller、service和dao的关系,又有新的理解
Controller
掌控前后端的交互
Service
提供业务逻辑
DAO
将底层数据进行加工提取等操作
三者关系
Controller-->Service-->DAO
即Controller包含Service,Service包含DAO
然后今天看了springMVC和REST接口,还是有点难度,看起来有点吃力,特别是springMVC,有点难理解
我的理解就是,通过注解的方式来定义Controller;然后再实现service;最后再用DAO层读取数据。
还是涉及配置文件,配置bean,不过对于bean的理解还有待加强!
在定义Controller时,使用REST接口实现前后端的交互,不过暂时没理解,明天继续看
然后今天写了一个简单的springMVC的helloworld例子,但是不知道如何运行,没有Test,没有main函数,应该怎样让这个小项目跑起来呢?
代码如下
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
spring-mvc-servlet.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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.yiibai.springmvc"/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB_INF/pages</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
root-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Empty -->
</beans>
HelloWorldController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Administrator on 2017/07/22.
*/
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("greeting","Hello Spring MVC");
return "helloworld";
}
}
helloworld.jsp
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC -HelloWorld</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
然后看教程是配置一下Run Configurations
配置maven
然后可以使用run运行这个项目,但是运行了很久也没完事,不知道我是不是搞错了
另外我自己安装配置完了Tomcat8,可是这个插件里只有Tomcat7:run
明天计划:先争取爬山不会累死,然后再想其他的,比如继续学习springMVC和REST啥的,感觉这个地方挺复杂的
问题:springMVC的maven项目怎么运行?
收获:springMVC的一点知识,写了一点代码
评论