Spring MVC四则运算

用疯狂软件写的Spring+MyBatis里的一点基础,写了JSR303校验的四则运算。

先看看效果,图一是默认效果,图二是输入错误效果。

为啥要写?因为这本书确实不错,案例也充沛,看完应该能入门,但是Jdk、Spring和使用的版本不同,会造成了一些Bug。。

比如Tomcat启动,启动一半报错了???或者启动后,报错,Tomcat启动失败???各种404或500???可能都以为是Tomcat的锅,重新加载Tomcat或换旧的Tomcat的。

Tomcat就想说:我只是一个服务器,你程序运行失败跟我啥事呀,为啥我被骂呀……

其实很多时候,是版本不兼容造成的,而这本书,只适合jdk1.8和它相匹配的Spring,比如4.2.0。

那今天我就用Jdk11.0.4和Spring5.2.0,Tomcat9.0.26来和大家一起鼓捣鼓捣。

下软件

首先先确定你想下的Jdk版本,最稳定的1.8或者最新的11都行。

Jdk下载地址: https://www.oracle.com/technetwork/java/javase/downloads/index.html

Tomcat只是一个服务器。配合Jdk用的,但太旧可能不支持新的Jdk,所以合适就行,最好与Jdk同时代。

tomcat下载地址: http://tomcat.apache.org/

Spring也是,和Jdk同时代的就行,否则可能出现兼容上的问题,但我用Jdk11和spring4.2很少出现大问题。

Spring下载地址: https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-milestone

点开像文档的图案,接着libs-release-local>>org>>spring

就可以下载你自己想要的版本,这个依据个人情况。

既然是JSR303校验,还应该下Hibernate Validator,我下的6.0

Hibernate的下载地址 http://hibernate.org/validator/releases/6.0/

注意:这些版本要互相兼容才行,不知道能不能兼容,多下几个其他版本就行

Spring里下载的libs包,我们只用1/3,你可以全导进编译器,也只导进jar包,名称最短的就行。因为可能全导进去,Tomcat可能会启动失败。

配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVCTest</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 让Spring MVC的前端控制器拦截所有请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置SpringMVC.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="io.github.jiema0217.controller"/>
<!-- 设置配置方案 -->
<mvc:annotation-driven/>
<!-- 使用默认的Servlet来响应静态文件 -->
<mvc:default-servlet-handler/>
<!-- 配置处理映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 视图解析器 -->
<bean id='viewResolver' class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

上面这两个配置xml。抄就完事,但也要改改,比如控制器去哪个包里找,视图解析器放的位置和你的想不想同等等。既然你点进来了,那说明这些要修改的地方,你是知道的。

以下代码请忽略我的英语命名。

controller

配置跳转路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package io.github.jiema0217.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.github.jiema0217.domain.CountResult;

//这些注解什么作用,你也应该看的出来
@Controller
public class CountController {

@RequestMapping(value = "/{formName}")
public String form(
@PathVariable String formName,
Model model){
CountResult count = new CountResult();
count.setSymbol(1);
model.addAttribute("count",count);
return formName;
}

@RequestMapping(value = "register",method=RequestMethod.POST)
public String register(
@Valid @ModelAttribute("count") CountResult count,
//书上就是@ModelAttribute,没有里面的值,但我的版本不加就会报错,建议还是加上妥
Errors errors,
Model model
) {
if (errors.hasErrors()){
return "main";
}
count.result(); //运算结果直接调用bean里的方法,有些不妥
model.addAttribute("count", count);
return "main";
}

}

domain

这里写的bean,获取jsp得到的x,y和符号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package io.github.jiema0217.domain;

import javax.validation.constraints.Pattern;


public class CountResult {

@Pattern(regexp="^-?\\d+(\\.\\d+)?$",message="请输入数字")
private String x;

private Integer symbol;

@Pattern(regexp="^-?\\d+(\\.\\d+)?$",message="请输入数字")
private String y;

private String result;

public String getX() {
return x;
}

public void setX(String x) {
this.x = x;
}

public Integer getSymbol() {
return symbol;
}

public void setSymbol(Integer symbol) {
this.symbol = symbol;
}

public String getY() {
return y;
}

public void setY(String y) {
this.y = y;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

//因为处理的很简单,所以在bean完成,但建议单独写一个类做数据处理
public void result() {
if (this.x !=null ||this.y != null) {
switch(this.symbol){
case 1:
this.result = String.valueOf(Double.parseDouble(this.x)
+ Double.parseDouble(this.y));
break;
case 2:
this.result = String.valueOf(Double.parseDouble(this.x)
- Double.parseDouble(this.y));
break;
case 3:
this.result = String.valueOf(Double.parseDouble(this.x)
* Double.parseDouble(this.y));
break;
case 4:
this.result = String.valueOf(Double.parseDouble(this.x)
/ Double.parseDouble(this.y));
break;
default :
break;
}
}
}
}

view

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSR303校验计算</title>
</head>
<body>
<form:form modelAttribute="count" method="post" action="register" >
<table>
<tr>
<td><form:input path="x"/></td>
<td>
<form:select path="symbol" >
<form:option value="1">+</form:option>
<form:option value="2">-</form:option>
<form:option value="3">*</form:option>
<form:option value="4">/</form:option>
</form:select>
</td>
<td><form:input path="y"/></td>
<td> = </td>
<td><form:input path="result"/></td>
<td><input type = "submit" value="提交"/></td>
</tr>
<tr>
<td><form:errors path="x" cssStyle= "color:red"/></td>
<td></td>
<td><form:errors path="y" cssStyle= "color:red"/></td>
</tr>
</table>
</form:form>
</body>
</html>
谢谢您对我的支持
0%