view技术简单了解
2019獨角獸企業重金招聘Python工程師標準>>>
?JSP & JSTL
View resolvers
InternalResourceViewResolver?and the ResourceBundleViewResolver常用來解析視圖,在web應用上下文里定義
<!-- the ResourceBundleViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
????<property name="basename" value="views"/>
</bean>
?
# And a sample properties file is uses (views.properties in WEB-INF/classes):
welcome.(class)=org.springframework.web.servlet.view.JstlView
welcome.url=/WEB-INF/jsp/welcome.jsp
?
productList.(class)=org.springframework.web.servlet.view.JstlView
productList.url=/WEB-INF/jsp/productlist.jsp
ResourceBundleViewResolver需要properties文件定義view名字映射的class,url;?ResourceBundleViewResolver可以使用不同類型視圖只用同一個Resovler
?
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
????<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
????<property name="prefix" value="/WEB-INF/jsp/"/>
????<property name="suffix" value=".jsp"/>
</bean>
InternalResourceBundleViewResolver可以用來解析jsps,最好是使用帶目錄的/WEB-INF/jsp/指定,避免直接訪問
?
Plain-old?JSPs versus JSTL
TLD在常用的jar中用來獲取綁定的數據
Using Spring’s form tag library
和其他的form和input標簽不同,spring form tag是spring web mvc 完整提供的,訪問命令對象和控制器處理的數據,使得jsps更方便
Configuration
The form tag library comes bundled in spring-webmvc.jar. The library descriptor is called spring-form.tld.
<%@?taglib?prefix="form"?uri="http://www.springframework.org/tags/form"?%>
The form tag
<form:form>
????<table>
????????<tr>
????????????<td>First Name:</td>
????????????<td><form:input path="firstName" /></td>
????????</tr>
????????<tr>
????????????<td>Last Name:</td>
????????????<td><form:input path="lastName" /></td>
????????</tr>
????????<tr>
????????????<td colspan="2">
????????????????<input type="submit" value="Save Changes" />
????????????</td>
????????</tr>
????</table>
</form:form>
firstName和lastName的值是由PageContext中的命令對象提供的
<form?method="POST">
????<table>
????????<tr>
????????????<td>First Name:</td>
????????????<td><input?name="firstName"?type="text"?value="Harry"/></td>
????????</tr>
????????<tr>
????????????<td>Last Name:</td>
????????????<td><input?name="lastName"?type="text"?value="Potter"/></td>
????????</tr>
????????<tr>
????????????<td?colspan="2">
????????????????<input?type="submit"?value="Save Changes"?/>
????????????</td>
????????</tr>
????</table>
</form>
其中form的變量名是返回對象的命令,如果要將form返回對象放入model在另一個名字下,你可以如下綁定名字
<form:form?commandName="user">
????<table>
????????<tr>
????????????<td>First Name:</td>
????????????<td><form:input?path="firstName"?/></td>
????????</tr>
????????<tr>
????????????<td>Last Name:</td>
????????????<td><form:input?path="lastName"?/></td>
????????</tr>
????????<tr>
????????????<td?colspan="2">
????????????????<input?type="submit"?value="Save Changes"?/>
????????????</td>
????????</tr>
????</table>
</form:form>
The input tag
The checkbox tag
將input tag 的?type?=checkbox
Eg:user preferences類
public?class?Preferences {
?
????private?boolean?receiveNewsletter;
????private?String[] interests;
????private?String favouriteWord;
?
????public?boolean?isReceiveNewsletter() {
????????return?receiveNewsletter;
????}
?
????public?void?setReceiveNewsletter(boolean?receiveNewsletter) {
????????this.receiveNewsletter = receiveNewsletter;
????}
?
????public?String[] getInterests() {
????????return?interests;
????}
?
????public?void?setInterests(String[] interests) {
????????this.interests = interests;
????}
?
????public?String getFavouriteWord() {
????????return?favouriteWord;
????}
?
????public?void?setFavouriteWord(String favouriteWord) {
????????this.favouriteWord = favouriteWord;
????}
}
Form.jsp ?like:
?
<form:form>
????<table>
????????<tr>
????????????<td>Subscribe to newsletter?:</td>
????????????<%--?Approach?1:?Property?is?of?type?java.lang.Boolean?--%>
????????????<td><form:checkbox?path="preferences.receiveNewsletter"/></td>
????????</tr>
?
????????<tr>
????????????<td>Interests:</td>
????????????<%--?Approach?2:?Property?is?of?an?array?or?of?type?java.util.Collection?--%>
????????????<td>
????????????????Quidditch:?<form:checkbox?path="preferences.interests"?value="Quidditch"/>
????????????????Herbology: <form:checkbox?path="preferences.interests"?value="Herbology"/>
????????????????Defence Against the Dark Arts: <form:checkbox?path="preferences.interests"?value="Defence Against the Dark Arts"/>
????????????</td>
????????</tr>
?
????????<tr>
????????????<td>Favourite Word:</td>
????????????<%--?Approach?3:?Property?is?of?type?java.lang.Object?--%>
????????????<td>
????????????????Magic:?<form:checkbox?path="preferences.favouriteWord"?value="Magic"/>
????????????</td>
????????</tr>
????</table>
</form:form>
?
?
There are 3 approaches to the checkbox tag which should meet all your checkbox needs.
·?Approach One - When the bound value is of type java.lang.Boolean, the input(checkbox) is marked as checked?if the bound value is true. The value attribute corresponds to the resolved value of the setValue(Object) value property.
·?Approach Two - When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as checked?if the configured setValue(Object) value is present in the bound Collection.
·?Approach Three - For any other bound value type, the input(checkbox) is marked as checked?if the configured setValue(Object) is equal to the bound value.
·?<tr>
·?????<td>Interests:</td>
·?????<td>
·?????????Quidditch: <input name="preferences.interests" type="checkbox" value="Quidditch"/>
·?????????<input type="hidden" value="1" name="_preferences.interests"/>
·?????????Herbology: <input name="preferences.interests" type="checkbox" value="Herbology"/>
·?????????<input type="hidden" value="1" name="_preferences.interests"/>
·?????????Defence Against the Dark Arts: <input name="preferences.interests" type="checkbox" value="Defence Against the Dark Arts"/>
·?????????<input type="hidden" value="1" name="_preferences.interests"/>
·?????</td>
·?</tr>
·?What you might not expect to see is the additional hidden field after each checkbox. When a checkbox in an HTML page is not?checked, its value will not be sent to the server as part of the HTTP request parameters once the form is submitted, so we need a workaround for this quirk in HTML in order for Spring form data binding to work. The checkbox tag follows the existing Spring convention of including a hidden parameter prefixed by an underscore ("_") for each checkbox. By doing this, you are effectively telling Spring that "the checkbox was visible in the form and I want my object to which the form data will be bound to reflect the state of the checkbox no matter what".
The checkboxes tag
不會全顯示,只有在運行時返回值在集合中array,list,map
<form:form>
????<table>
????????<tr>
????????????<td>Interests:</td>
????????????<td>
????????????????<%-- Property is of an array or of type java.util.Collection --%>
????????????????<form:checkboxes path="preferences.interests" items="${interestList}"/>
????????????</td>
????????</tr>
????</table>
</form:form>
The radiobutton tag
<tr>
????<td>Sex:</td>
????<td>
????????Male: <form:radiobutton path="sex" value="M"/> <br/>
????????Female: <form:radiobutton path="sex" value="F"/>
????</td>
</tr>
The radiobuttons tag
運行時顯示,In the case where you use a Map, the map entry key will be used as the value and the map entry’s value will be used as the label to be displayed.
?
<tr>
????<td>Sex:</td>
????<td><form:radiobuttons path="sex" items="${sexOptions}"/></td>
</tr>
The select tag
The option tag
The options tag
The textarea tag
The hidden tag
The errors tag
public?class?UserValidator implements?Validator {
?
????public?boolean?supports(Class candidate) {
????????return?User.class.isAssignableFrom(candidate);
????}
?
????public?void?validate(Object obj, Errors errors) {
????????ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
????????ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
????}
}
The form.jsp?would look like:
<form:form>
????<table>
????????<tr>
????????????<td>First Name:</td>
????????????<td><form:input?path="firstName"?/></td>
????????????<%--?Show?errors?for?firstName?field?--%>
????????????<td><form:errors?path="firstName"?/></td>
????????</tr>
?
????????<tr>
????????????<td>Last Name:</td>
????????????<td><form:input?path="lastName"?/></td>
????????????<%--?Show?errors?for?lastName?field?--%>
????????????<td><form:errors?path="lastName"?/></td>
????????</tr>
????????<tr>
????????????<td?colspan="3">
????????????????<input?type="submit"?value="Save Changes"?/>
????????????</td>
????????</tr>
????</table>
</form:form>
·?path="*" - displays all errors
·?path="lastName" - displays all errors associated with the lastName field
·?if path is omitted - object errors only are displayed
顯示一系列錯誤在網頁的上面,顯示具體的錯誤在field旁邊
<form:form>
????<form:errors path="*" cssClass="errorBox" />
????<table>
????????<tr>
????????????<td>First Name:</td>
????????????<td><form:input path="firstName" /></td>
????????????<td><form:errors path="firstName" /></td>
????????</tr>
????????<tr>
????????????<td>Last Name:</td>
????????????<td><form:input path="lastName" /></td>
????????????<td><form:errors path="lastName" /></td>
????????</tr>
????????<tr>
????????????<td colspan="3">
????????????????<input type="submit" value="Save Changes" />
????????????</td>
????????</tr>
????</table>
</form:form>
HTTP Method Conversion http方法轉變
Rest的重要原則是使用統一接口,意味著所有的資源都能被這四種方法操作(get,put,post,delete),對于每一個方法,http規格定義了明確的語義,例如,get必須是安全的操作,沒有其他影響,,PUT和delete行為是idempotent,你可以一次又一次重復這些操作,但最終結果是一樣的,http定義這四種方式時,html只支持兩種,get和post.幸運的是有兩種變通方案:你可以用javascript使用put和delete,后者簡單使用post方法作為參數(在html form中作為隱藏區),之后這是Spring’s HiddenHttpMethodFilter做的,是基本的servlet過濾器,可以和任何web framework工作,簡單將filter加入web.xml,隱藏參數的方法將被轉換為相關的http方法請求
<form:form method="delete">
????<p><input type="submit" value="Delete Pet"/></p>
</form:form>
?
是一個http post,實際是一個delete方法隱藏在一個請求參數中,將會被HiddenHttpMethodFilter:
<filter>
????<filter-name>httpMethodFilter</filter-name>
????<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
?
<filter-mapping>
????<filter-name>httpMethodFilter</filter-name>
????<servlet-name>petclinic</servlet-name>
</filter-mapping>
相應的@C otroller方法如下
@RequestMapping(method = RequestMethod.DELETE)
public String deletePet(@PathVariable?int ownerId, @PathVariable?int petId) {
????this.clinic.deletePet(petId);
????return "redirect:/owners/" + ownerId;
}
HTML5 Tags
Velocity & FreeMarker
ur web application will need to include velocity-1.x.x.jar?or freemarker-2.x.jar
轉載于:https://my.oschina.net/iioschina/blog/670601
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的view技术简单了解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 获取方法所在的命名空间 类名 方法
- 下一篇: Redis实现关注关系