Spring MVC框架個(gè)人詳解題目:注冊(cè)頁(yè)面@pathVariable@ModelAttribute注解【詩(shī)書(shū)畫(huà)唱】
概括:
1、創(chuàng)建一個(gè)注冊(cè)頁(yè)面,提交后在controller類(lèi)中打印出注冊(cè)信息
2、輸入url地址:http://localhost:xxxx/xxxx/money/page/sunday,在controller類(lèi)中打印出money和sunday。用pathVariable注解實(shí)現(xiàn)
3、創(chuàng)建一個(gè)商品類(lèi),包含名稱和價(jià)格,創(chuàng)建一個(gè)商品新增頁(yè)面add.jsp,當(dāng)點(diǎn)擊新增按鈕時(shí),跳轉(zhuǎn)到show.jsp頁(yè)面,在這個(gè)頁(yè)面上顯示輸入的商品名稱和價(jià)格。要求使用@ModelAttribute進(jìn)行處理。
?pathVariable的使用方法?
?使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法?
設(shè)置訪問(wèn)路徑跳轉(zhuǎn)到 Register.jsp界面
product:生產(chǎn)品(一般寫(xiě)代碼時(shí),是當(dāng)作“商品”的意思)
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 START
本來(lái)想用toProductSuccess和ProductSuccess的
命名的,更直觀,但按照題目要求就分別替換成
toShow和show
*/


1、創(chuàng)建一個(gè)注冊(cè)頁(yè)面,提交后在controller類(lèi)中打印出注冊(cè)信息



package com.SSHC.bean;
public class User {
? ? private String name;
? ? private String Sex;
? ? private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return Sex;
}
public void setSex(String Sex) {
this.Sex = Sex;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
? ?
}

/**
?*?
?* pathVariable的使用方法?
?*?
?*?
?*?
?* 使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法?
?* */
package com.SSHC.controller;
import org.springframework.stereotype.Controller;
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.RequestParam;
import com.SSHC.bean.User;
@Controller
public class UserController {
/*pathVariable的使用方法 START:
下面的MyMoney,weekend是可變的變量名,
自己根據(jù)需要去取名字
?
?String是訪問(wèn)路徑中傳來(lái)的內(nèi)容的數(shù)據(jù)類(lèi)型,
?如果是int類(lèi)型,就聲明為int
?
?return "pathVariable"
?表示跳轉(zhuǎn)到pathVariable.jsp界面
?
http://localhost:8080/SpringHomework6/money/page/sunday*/
@RequestMapping(value = "{MyMoney}/page/{weekend}")
public String reg(@PathVariable("MyMoney") String n,
@PathVariable("weekend")String t){
System.out.println(n);
System.out.println(t);
return "pathVariable";
}
/*pathVariable的使用方法 END*/
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 START*/
@RequestMapping(value = "toRegisterSuccess")
public String doReg(@ModelAttribute("_user") User u){
//request.setAttribute("_user",u);
return "RegisterSuccess";
}
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 END*/
}

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size: 25px;
? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? #form{ margin: 0 auto;
? ? ? ? ? ? border:1px solid red;?
? ? ? ? ? ? width: 800px;
? ? ? ? ? ? height: 800px;
? ? ? ?
? ? ? ? ? ? }
? ?td{text-align:
center;vertical-align: middle;}
? ? ? ? </style>
? ? </head>
? ? <body>
? ? <div id='form' >
? ? ? ? <form action="toRegisterSuccess" method="post" >
? ? ? ? <table border="1px" align="center">
? ? ? ? <tr>
? ? ? ? <td>
? ? ? ? 賬號(hào)名:</td>? ? <td>? ?<input type="text" name="name" placeholder="請(qǐng)輸入姓名"/>
? ? ? ? ? ? </td><tr>
? ? ?<tr>? <td>性別:</td>? ?
? ? ?<td>? <input type="radio" name="Sex" value="男"/>男
? ? ? ? ? ? <input type="radio" name="Sex" value="女"/>女
? ? ? ? ? </td>? <br></tr>
? <tr>
<td>密碼:</td><td><input type="password" name="pwd" placeholder="請(qǐng)輸入密碼" />
? ? ? ? ? ? <br></td></tr>
? ? ? ? ? <tr> <td colspan="2">?
? ? ? ? ? <input type="submit" value="注冊(cè)" /></td></tr>
? ? ? </table>
? ? ? ? </form>
? ? ? ? </div>
? ? </body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>${_user.name }</h1>
? ? ? ? <h1>${_user.Sex }</h1>
? ? ? ? <h1>${_user.pwd }</h1>
? ? ? ??
? ? </body>
</html>

<?xml version="1.0" encoding="UTF-8"?>
<!-- CTRL+F: -->
<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-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">? ? ? ? ? ? ? ? ? ? ? ?
? ? <!-- 掃描指定的包和它的子包中的所有java類(lèi) -->
? ? <!-- 只要是創(chuàng)建在com.SSHC.controller包下面的java類(lèi),只要給它添加了@Controller注解
? ? ? ? ? ? ? ? ? 那么這個(gè)java類(lèi)就變成了springmvc中的一個(gè)controller -->
? ? <context:component-scan base-package="com.SSHC.controller"/>
? ? <!-- 默認(rèn)注冊(cè)RequestMappingHandlerMapping和RequestMappingHandlerAdapter類(lèi) -->
? ? <mvc:annotation-driven />
? ? <!-- jsp引用外部js,css等靜態(tài)資源的解決方法(和上面的標(biāo)簽必須同時(shí)出現(xiàn),否則無(wú)法訪問(wèn)url) -->
? ? <mvc:default-servlet-handler />
? ? <!-- 配置視圖名稱解析器 -->
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"?
? ? ? ? ? ? id="internalResourceViewResolver">
? ? ? ? <!-- 前綴 -->
? ? ? ? <!-- 將所有的jsp文件存放在/WEB-INF/my/目錄下 -->
? ? ? ? <property name="prefix" value="/WEB-INF/" />
? ? ? ? <!-- 后綴 -->
? ? ? ? <property name="suffix" value=".jsp" />
? ? ? ? <!-- 優(yōu)先級(jí)設(shè)定 -->
? ? ? ? <property name="order" value="10"></property>
? ? </bean>
? ??
? ??
? ? <!-- 設(shè)置訪問(wèn)路徑跳轉(zhuǎn)到 Register.jsp界面 START-->
? <!-- http://localhost:8080/SpringHomework6/toRegister -->
? ? <mvc:view-controller path="/toRegister" view-name="Register" />
? <!--? <mvc:view-controller path="/toRegisterSuccess" view-name="RegisterSuccess" />-->
? ? <!-- 設(shè)置訪問(wèn)路徑跳轉(zhuǎn)到 Register.jsp界面 END-->
?<!-- 設(shè)置訪問(wèn)路徑跳轉(zhuǎn)到 add.jsp界面 START-->
? <!-- http://localhost:8080/SpringHomework6/toAdd -->
? ? <mvc:view-controller path="/toAdd" view-name="add" />
??
</beans>

<?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>Spring6</display-name>
? <!-- controller中文亂碼處理,注意一點(diǎn):要配置在所有過(guò)濾器的前面 -->
? <filter>
? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
? ? <init-param>
? ? ? <param-name>encoding</param-name>
? ? ? <param-value>utf-8</param-value>
? ? </init-param>
? </filter>
? <filter-mapping>
? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? <url-pattern>/*</url-pattern>
? </filter-mapping>
? <!-- springmvc框架配置 -->
? <!-- DispatcherServlet是前置控制器,配置在web.xml文件中的。
? ? ? ? ? ? 攔截匹配的請(qǐng)求,Servlet攔截匹配規(guī)則要自已定義,把攔截下來(lái)的請(qǐng)求,
? ? ? ? ? ? 依據(jù)相應(yīng)的規(guī)則分發(fā)到目標(biāo)Controller來(lái)處理,
? ? ? ? ? ? 是配置spring MVC的第一步。 -->
? <servlet>
? ? ? <servlet-name>springmvc</servlet-name>
? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? <init-param>
? ? ? ? ? <!-- 覆蓋默認(rèn)配置文件{servlet-name}-servlet.xml -->
? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
? ? ? </init-param>
? ? ? <load-on-startup>1</load-on-startup>
? </servlet>
? <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-list>
</web-app>


http://localhost:8080/SpringHomework6/toRegister
http://localhost:8080/SpringHomework6/money/page/sunday
http://localhost:8080/SpringHomework6/toAdd

register:注冊(cè)
pathVariable:可變路徑
product:生產(chǎn)品(一般寫(xiě)代碼時(shí),是當(dāng)作“商品”的意思)
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 START
本來(lái)想用toProductSuccess和ProductSuccess的
命名的,更直觀,但按照題目要求就分別替換成
toShow和show
*/


2、輸入url地址:http://localhost:xxxx/xxxx/money/page/sunday,在controller類(lèi)中打印出money和sunday。用pathVariable注解實(shí)現(xiàn)






運(yùn)行訪問(wèn)路徑后的效果:

3、創(chuàng)建一個(gè)商品類(lèi),包含名稱和價(jià)格,創(chuàng)建一個(gè)商品新增頁(yè)面add.jsp,當(dāng)點(diǎn)擊新增按鈕時(shí),跳轉(zhuǎn)到show.jsp頁(yè)面,在這個(gè)頁(yè)面上顯示輸入的商品名稱和價(jià)格。要求使用@ModelAttribute進(jìn)行處理。


package com.SSHC.bean;
public class Product {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}

/**
?*?
?* 使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法?
?* */
package com.SSHC.controller;
import org.springframework.stereotype.Controller;
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.RequestParam;
import com.SSHC.bean.Product;
@Controller
public class ProductController {
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 START
本來(lái)想用toProductSuccess和ProductSuccess的
命名的,更直觀,但按照題目要求就分別替換成
toShow和show
*/
@RequestMapping(value = "toShow")
public String doReg(@ModelAttribute("_Product")?
Product o){
/*我傳值時(shí),常常命名變量為o,表示object,個(gè)人認(rèn)為會(huì)更通用些
* 同時(shí)表單提交的跳轉(zhuǎn)方法一般我都習(xí)慣在
* controller中設(shè)置,在配置文件中設(shè)置有時(shí)會(huì)
* 報(bào)"不支持POST"的錯(cuò),而且有時(shí)難傳值
*?
*?
* */
System.out.println(o.getName());
System.out.println(o.getPrice());
return "show";
}
/*使用ModelAttribute表單提交打印出
其傳到跳轉(zhuǎn)到的jsp界面的方法 END*/
}


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size: 25px;
? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? #form{ margin: 0 auto;
? ? ? ? ? ? border:1px solid red;?
? ? ? ? ? ? width: 800px;
? ? ? ? ? ? height: 800px;
? ? ? ?
? ? ? ? ? ? }
? ?td{text-align:
center;vertical-align: middle;}
? ? ? ? </style>
? ? </head>
? ? <body>
? ? <div id='form' >
? ? ? ? <form action="toShow" method="post" >
? ? ? ? <table border="1px" align="center">
? ? ? ? <tr>
? ? ? ? <td>
? ? ? ? 商品名稱:</td>? ? <td>? ?<input type="text" name="name" placeholder="請(qǐng)輸入商品名"/>
? ? ? ? ? ? </td><tr>
? ? ?
? <tr>
<td>商品價(jià)格:</td><td><input type="text" name="price" placeholder="請(qǐng)輸入商品價(jià)格" />
? ? ? ? ? ? <br></td></tr>
? ? ? ? ? <tr> <td colspan="2">?
? ? ? ? ? <input type="submit" value="新增" /></td></tr>
? ? ? </table>
? ? ? ? </form>
? ? ? ? </div>
? ? </body>
</html>



