在写项目过程中我们会遇到一些奇怪的问题:
比如:
1. maven项目无法创建新项目,即使通过加大虚拟内存还是不行,只有添加新的workSpace才有效果。
2.我们在使用maven依赖的时候,有时候会无法索引出想要的库。我们可以通过windows--show view -- Maven Repositories
进行重新加载:
3.我们在升级web.xml的过程中发现,如果将版本提升到version="3.1",会有波浪线提示,其实就是版本不匹配。
此时,我们可以通过手动更新maven容器。
则会提示无法更新到3.1
所以需要手动配置:
取消勾选后会出现.settings包
然后再在pom.xml中加入一句话,进行加载:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
然后在重新更新maven,所有报错都会消失。
4. 更新jsp的创建模板:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tags" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
</style>
</head>
<body>
</body>
<script type="text/javascript">
</script>
</html>
Form表单
首先,对web.xml进行升级改造到3.1版本
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 重新定义springmvc文件存放的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 会直接读取resources文件目录下的配置 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- tomcat启动的时候会重新加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
其中,有几个设置比较重要:
重新定义springmvc文件存放的位置,会直接读取resources文件目录下的配置
<!-- 重新定义springmvc文件存放的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 会直接读取resources文件目录下的配置 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
tomcat启动的时候会重新加载
<!-- tomcat启动的时候会重新加载 -->
<load-on-startup>1</load-on-startup>
第二步:我们需要对表单进行初始化,就是我们在一登录表单界面,会展示的一些信息,这些信息是从后台读取的。
先设置一个userForm作为提交的用户表单:
package com.form.forms;
import java.util.Date;
import java.util.List;
public class UserForm {
private String id;
private String name;
private String password;
private String repeatPassword;
private int age;
private String phoneNumber;
private String gender;
private List<String> hobby;
private String country;
private String remark;
private Date birthday;
private String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepeatPassword() {
return repeatPassword;
}
public void setRepeatPassword(String repeatPassword) {
this.repeatPassword = repeatPassword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
然后,通过userController.java进行页面展示:通过setModel ,设置属性值,即是对表单进行初始化操作,其实这些信息都是可以从数据库中获取。
package com.form.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.form.forms.UserForm;
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("show")
public String show(Model model) {
UserForm userForm = new UserForm();
userForm.setId("zhm");
this.setModel(model);
model.addAttribute("user",userForm);
return "addUser";
}
private void setModel(Model model) {
//set gender
Map<String, String> genderMap = new HashMap<String, String>();
genderMap.put("man", "Man");
genderMap.put("woman", "Woman");
//set Hobby
Map<String, String> hobbyMap = new HashMap<String, String>();
hobbyMap.put("dancing", "Dancing");
hobbyMap.put("singing", "Singing");
hobbyMap.put("printing", "Printing");
//Country
Map<String, String> countryMap = new HashMap<String, String>();
countryMap.put("china", "China");
countryMap.put("japan", "Japan");
countryMap.put("us", "US");
model.addAttribute("genderMap", genderMap);
model.addAttribute("hobbyMap", hobbyMap);
model.addAttribute("countryMap", countryMap);
}
}
第三步:我们需要jsp进行web页面展示:
解析:
action = 响应路径 、 commandName =表单名称 、 method == 提交方式 、 commandName=提交的表单名称 、 modelAttribute=表单属性读取并设置
form:password = 非明文密码 、 form:input = 手动输入框 、 form:radiobuttons = 单选框 、form:checkboxes = 复选框 、form:select = 下拉框 、form:textarea = 文本框
${genderMap } : 这种表达方式是jsp用于读取modelAttribute中的内容进行展示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tags" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add User</title>
<style type="text/css">
</style>
</head>
<body>
<h2> Add User</h2>
<!-- 表单的名字: action = 响应路径 , commandName =表单名称 ,method == 提交方式 -->
<form:form method="POST" action="add" commandName="userForm" modelAttribute="userForm">
<table>
<tr>
<td><form:label path="id">ID</form:label></td>
<td><form:label path="id" maxlength="8"/></td>
</tr>
<tr>
<td><form:label path="name">name</form:label></td>
<td><form:label path="name" maxlength="8"/></td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:password path="password"/><form:errors path="password"/></td>
</tr>
<tr>
<td><form:label path="repeatPassword">Repeat Password</form:label></td>
<td><form:password path="repeatPassword"/><form:errors path="repeatPassword"/></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age"/></td>
</tr>
<tr>
<td><form:label path="phoneNumber">Phone Number</form:label></td>
<td><form:input path="phoneNumber"/><form:errors path="phoneNumber"/></td>
</tr>
<tr>
<td><form:label path="gender">Gender</form:label></td>
<td>
<%-- <form:radiobutton path="gender" value="man"/>男
<form:radiobutton path="gender" value="woman"/>女 --%>
<form:radiobuttons path="gender" items="${genderMap }"/>
</td>
</tr>
<tr>
<td><form:label path="hobby">Hobby</form:label></td>
<td>
<%-- <form:checkbox path="hobby" value="dancing"/>Dancing
<form:checkbox path="hobby" value="singing"/>Singing
<form:checkbox path="hobby" value="Printing"/>Printing --%>
<form:checkboxes items="${hobbyMap }" path="hobby"/>
</td>
</tr>
<tr>
<td><form:label path="country">Country</form:label></td>
<td>
<%-- <form:select path="country">
<form:option value="china">China</form:option>
<form:option value="japan">Japan</form:option>
<form:option value="us">US</form:option>
</form:select> --%>
<form:select path="country" items="${countryMap }"/>
</td>
</tr>
<tr>
<td><form:label path="remark">Remark</form:label></td>
<td><form:textarea path="remark"/></td>
</tr>
<tr>
<td><form:label path="birthday">Birthday</form:label></td>
<td><form:input path="birthday"/></td>
</tr>
<tr>
<td><form:label path="email">Email</form:label></td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td colspan="2"><form:button>Submit</form:button></td>
</tr>
</table>
</form:form>
</body>
<script type="text/javascript">
</script>
</html>
运行tomcat,输入我们配置好路径:http://localhost:8080/SpringMVC-FormProject/user/show
所以这样就是属于表单初始化设置。其实,同样我们也可以在html或者jsp页面进行初始化设置。效果是一样.