发表于: 2018-01-12 16:50:12
2 737
今天完成的事情:
1.学习了一点html的基础
(1)
<h>表示标题<h>
<h1>表示最大的标题<h1>
<h6>表示最小的标题<h6>
(2)
一个完整的html文件,应该至少包含html元素,body元素,以及里面的内容。
<html> 表示这是一个html文件
<body> 定义文档的主体,包含文档所有内容
<h1>标题</h1>
</body>
</html>
(3)
起始符号为: <%@
终止符号为: %>
这表示是指令,主要用来提供整个JSP 网页相关的信息,并且用来设定JSP网页的相关属性,
例如:网页的编码方式、语法、信息等。
目前有三种指令:page、include 和taglib,每一种指令都有各自的属性。
page 指令是最复杂的JSP指令,它的主要功能为设定整个JSP 网页的属性和相关功能。
include 指令表示:在JSP 编译时插入一个包含文本或代码的文件,这个包含的过程是静态的,而包含的文件可以是JSP 网页、HTML 网页、文本文件,或是一段Java 程序。
taglib 指令能够让用户自定义新的标签。
(4)
写在开始标签里的 align="center" 就叫属性
align 是属性名
center 是属性值
属性值应该使用双引号括起来
<h1 >未设置居中的标题</h1>
<h1 align="center">居中标题</h1>
(5)
<p>标签即表示段落是paragraph的缩写自带换行效果,不在p标签的文本不会自动换行。
(6)
<b>
<strong>
都可以用来实现粗体的效果
b是bold的缩写,仅仅表示该文本是粗体的,并不暗示这段文字的重要性
strong虽然也是粗体,但是更多的是强调语义上的加重,提醒用户该文本的重要性。 在SEO(搜素引擎优化)的时候,也更加容易帮助用户找到重点的内容
推荐使用strong
<p>无粗体效果</p>
<b>b标签粗体效果</b>
<br/>
<strong>strong标签粗体效果</strong>
(7)
有时候,需要在网页上显示代码,比如java代码
就需要用到pre:
<p>这里是没有用预格式的情况:</p>
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
<br/>
<br/>
<p>使用预格式的情况:</p>
<pre>
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
</pre>
运行结果:
这里是没有用预格式的情况:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
使用预格式的情况:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
(7)
<del>即删除标签
<p>无删除效果</p>
<del>使用del标签实现的删除效果</del>
运行结果:
无删除效果
使用del标签实现的删除效果
(8)
<a>标签即用来显示超链
完整元素是
<a href="跳转到的页面地址">超链显示文本</a>
用12306来做演示的话就是:
<a href="http://www.12306.com">12306</a> 当前页面打开网站
<a href="http://www.12306.com" target="_blank">http://www.12306.com</a> 新开网页打开文件
(9)
<input type="reset"> 重置按钮 可以把输入框的改动复原,并且在Demo中试验过了,很好用
(10)
<hr>加一条水平线。
2.学习JSTL
(1) jstl是一个jsp标签库;为了抑制在jsp里面写大量java代码,所用jstl标签来实现java里面的逻辑调用;jstl是一个实现web应用程序中常见的通用功能的定制标记库集。
jstl包含5个标签库:core标签库,xml标签库,国际化与格式化标签库和SQL标签库。
jstl使用的话,需要导入jar包
在项目中我只用到了core,原文中的引用:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
在我的项目里面,在获得学生信息列表的时候加了一个判断,判断是否有数据:list.jsp
<body>
<h2>学生信息表</h2>
<c:if test="${students == null}">
<h3> 没有信息</h3>
</c:if>
引用的是core的if 方法。
如果${student == null}是true的话,name会打印if中间的的部分。
并且,接下来用到了core的foreach 方法:
<c:forEach items="${students}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.qq}</td>
<td>${stu.name}</td>
<td>${stu.school}</td>
<td>${stu.type}</td>
<td>${stu.say}</td>
<td>${stu.create_at}</td>
<td>${stu.update_at}</td>
<td><a href="${pageContext.request.contextPath}/updStu/${stu.id}">编辑</a> </td>
<td><a href="${pageContext.request.contextPath}/delStu/${stu.id}">删除</a></td>
</tr>
</c:forEach>
因为students是一个结果集,通过这样的遍历来输出所有结果。
(2)这样的话,反过来看看list.jsp对应的controller语句:
@RequestMapping(value = "/stus",method = RequestMethod.GET)
public String getAll(Model model){
List<Student> students = studentService.getAll();
model.addAttribute("students",students);
return "list";
}
因为我对model的理解有问题,所以卡了一会儿,在和师兄交流之后,得知了model的含义:
变量students已经获得了值,我想要让这个值在list.jsp页面显示出来,“students”对应的jsp页面的
<c:if test="${students == null}">
通过model的addAttribute将变量students的值传进了“students”,也就是传进了前台页面。这样一来,传值就成功了,就可以在前端页面获取到students的值了。
光说不练假把式,我自己写了一个小小的Demo:
这是在controller类里面添加的新方法。
@RequestMapping(value = "/see",method = RequestMethod.GET,produces = "text/html;character=UTF-8")
public String see(Model model){
int a = 100;
model.addAttribute("seee",a);
return "watch";
}
下面是为了上面的测试写的jsp页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${seee}
</body>
</html>
然后是运行结果:
成功,至此,可以说,知道这个model到底是咋回事儿了。
(3)接下来试着去理解增加学生的方法:
@RequestMapping(value = "/addStu",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("student",new Student());
return "input";
}
这块儿的Map之前有点了解,是一种集合框架,也可以理解为一种容器,这里相当于用键值对的方式保存了new Student(),然后通过put方法映射到前端页面。
在前端页面里面涉及到了新的引用from:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form> 标签用于为用户输入创建 HTML 表单。
表单能够包含 input 元素,比如文本字段、复选框、单选框、提交按钮等等。
表单还可以包含 menus、textarea、fieldset、legend 和 label 元素。
表单用于向服务器传输数据。
之前通过map.put过来的数值是一个空的student类型数据,对应下面的student;action后面那些的意思是向何处提交数据。
<form:form action="${pageContext.request.contextPath}/addStu" method="post" modelAttribute="student">
空的数据过来之后,这边页面会跳转到输入学生信息的界面,再输入学生信息之后,点击提交,这时候又会进入controller:见下面第二个图
<form:form action="${pageContext.request.contextPath}/addStu" method="post" modelAttribute="student">
姓名:<form:input path="name"/><br/>
QQ:<form:input path="qq"/><br/>
类型:<form:input path="type"/><br/>
学校:<form:input path="school"/><br/>
宣言:<form:input path="say"/><br/>
创建时间:<form:input path="create_at"/><br/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form:form>
@RequestMapping(value = "/addStu",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("student",new Student());
return "input";
}
@RequestMapping(value = "/addStu",method = RequestMethod.POST )
public String create(Integer id,String name, Integer qq, String school, String type, String say, Integer create_at, Integer update_at, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
studentService.create(id,name,qq,school,type,say,create_at,update_at);
return "redirect:/stus";
}
点击提交之后,会同时执行两个动作:一个是给空的student注入值(在inputjsp界面填入的信息),同时create方法会得到这些数据创建新的学生信息并跳转到stus url 不知道我这么理解对不对?求师兄解答。这个问题和曲师兄探讨了一下,但是结论并不是很肯定
上面的问题还没弄完,明天继续。
3.配置文件的详解:
我就直接copy上来了,今天边查边注释,都在代码里了
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
<h2>Hello World!</h2>
<!--a标签用于定义超链接,从一张页面链接跳转到另一张-->
<!--a元素最重要的是href属性,它知识链接的目标。-->
<!--${pageContext.request.contextPath}相当于http://localhost:8080/项目名-->
<a href="${pageContext.request.contextPath}/stus">学生信息~</a>
<br/><!--简单理解为换行-->
<br/>
<br/>
<a href="${pageContext.request.contextPath}/seeStu">查找页面</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%><!--%@%设定jsp页面的相关属性和相关功能。contentType定义用户的浏览器或相关设备如何显示将要加载的数据,
或者如何处理将要加载的数据,后面跟的是HTML文档-->
<!--两个%表示这里面的文本不是普通直接输出到客户端的文本而是需要服务器来解释的-->
<%
List<String> words = new ArrayList<String>();
words.add("today");
words.add("is");
words.add("a");
words.add("great");
words.add("day");
%>
<table width="100px" align="center" border="1" cellspacing="0">
<%for (String word : words) {%>
<!--tr表示行,td表示列-->
<tr>
<td><%=word%></td>
</tr>
<%}%>
</table>
<!--插入另个页面-->
<%@include file="WEB-INF/views/footer.jsp" %>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name><!-- 表述性文字,可删除-->
<!--welcome pages-->
<welcome-file-list><!--欢迎界面,俗称默认界面,如果输入的url没有包含servlet,jsp等资源,此时就会显示这个jsp界面-->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置springmvc DispatcherServlet-->
<servlet><!--用来声明一个servlet的数据-->
<servlet-name>springMVC</servlet-name><!--该servlet的名称-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--该servlet类的全路径-->
<init-param><!--初始化参数-->
<!--配置dispatcher.xml作为mvc的配置文件-->
<param-name>contextConfigLocation</param-name><!--参数的名称-->
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value><!--参数的地址,或者叫取值地点-->
</init-param>
<load-on-startup>1</load-on-startup><!--表示启动后加载的优先级很高-->
<async-supported>true</async-supported><!--设置异步处理的支持 默认是false,设置为true,表示打开异步处理支持-->
</servlet>
<servlet-mapping><!--用来定义servlet所对应的url-->
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern><!--指定servlet所对应的url,/表示任何url-->
</servlet-mapping>
<!--把applicationContext.xml加入到配置文件中-->
<context-param><!--全局范围内环境参数初始化-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener><!--设定listener接口-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--此文件负责整个mvc中的配置-->
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--静态资源映射-->
<!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
<mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<!-- 自动扫描装配 -->
<context:component-scan base-package="example.controller"/>
</beans>
<%@ page import="java.util.Date" %>
<!--指定标签c,值为后面的uri,调用的时候如第8行所示-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><!--定义文档的头部-->
<title>Index</title><!--定义标题,是head部分中唯一必须的元素-->
<!--link定义文档与外部资源的关系,这里是链接一个外部样式表,最常用-->
<link rel="stylesheet" type="text/css" href="<c:url value="/css/test.css"/> "/>
</head>
<body>
<%=new Date().toLocaleString()%><br>
<p>涉及到了考试结果</p>
</body>
</html>
<hr><!--创建水平线-->
<p style="text-align:center">copyright@2016
</p><!--p元素会在段落前后创建一些空白-->
今天遇到的问题
就是这些配置文件了,涉及到的东西非常多,各种标签的含义之类的就搜了很久,再加上之后的对jsp页面以及controller的理解免费了很长时间
今天的收获
可以说是很多干货吧,理解了比较多的标签和一些神奇的方法。
明天计划的事情
把剩余的那些controller类的方法以及对应的jsp理解完,时间允许的话,就写Demo,因为目前的项目是jdbc,明天可以的话就写mybatis整合。传说中的ssm
最后,辛苦我帅帅的师兄审核
评论