Spring MVC框架介紹和表單處理,全局的亂碼處理,個(gè)人解決404報(bào)錯(cuò)的經(jīng)驗(yàn)【詩書畫唱】
本篇專欄目錄:
邊看教程視頻邊做記錄的部分
常見的框架
使用在Java Web項(xiàng)目的Spring MVC框架的關(guān)鍵是配置文件的base-package的值,其值代表其控制器類要放的包名
配置視圖名稱解析器
這個(gè)return?”index“的意思是:在當(dāng)前配置文件的設(shè)置下表示要return跳轉(zhuǎn)的界面是WEB-INF目錄下的index.jsp
運(yùn)行開啟服務(wù)器后,輸入圖中的訪問地址就有如圖的運(yùn)行效果
很多都是先從實(shí)現(xiàn)簡單的效果開始,比如打印出”Hello World“,無論學(xué)習(xí)什么框架,vue,微信小程序等等都是先從最簡單的部分開始,實(shí)現(xiàn)后就是開啟了”新世界的大門“,因?yàn)樽铋_始最關(guān)鍵的是環(huán)境的搭建等等。
關(guān)于Spring MVC框架的配置文件的注意事項(xiàng)
覆蓋默認(rèn)配置文件servlet-name的param-name和param-value設(shè)置?
更方便的配置文件的param-name和param-value設(shè)置?
配置文件servlet-name等的設(shè)置?
下面的是更常用關(guān)于Spring MVC框架的web.xml的配置
@RequestMapping(“XXX”)中的XXX有時(shí)在某方面可以理解為類似于訪問路徑中的“包名”的作用
在Spring MVC框架中跳轉(zhuǎn)到WEB-INF目錄下的jsp文件的界面的方法(WEB-INF目錄下的界面有時(shí)不可以直接訪問等等,要通過控制器controller這個(gè)類似于servlet的文件的轉(zhuǎn)發(fā)來實(shí)現(xiàn)跳轉(zhuǎn)
在Spring MVC框架中的form框架的表單提交的話,也是要提交到要跳轉(zhuǎn)到要接收提交過來的信息的界面。@RequestMapping中命名的值的話就是form中action中的值,就是提交表單時(shí)要跳轉(zhuǎn)到的界面,本圖中要注意option的value的賦值
圖中我框起來的部分的內(nèi)容的意思是:自動把jsp表單提交的內(nèi)容封裝到u這個(gè)User類中,通過打印語句來查看是否封裝成功
在web.xml文件中的form表單提交時(shí)的數(shù)據(jù)的全局的亂碼處理(在web.xml中配置亂碼過濾器)
個(gè)人理解:Spring MVC框架很”聰明“,當(dāng)你在form表單中的name的賦值和bean中實(shí)體類對應(yīng)的屬性名一致時(shí),提交時(shí)name的賦值中對應(yīng)的value的賦值就會賦值給實(shí)體類對應(yīng)的屬性名。因?yàn)槿绱?,要做注冊等的功能的?shí)現(xiàn)的代碼就會變得更少。
在控制器controller中設(shè)置跳轉(zhuǎn)到一個(gè)jsp界面中用EL表達(dá)式獲取其值的方法用put方法
個(gè)人的項(xiàng)目經(jīng)驗(yàn)
項(xiàng)目例子記錄
個(gè)人經(jīng)驗(yàn):?
?“http://localhost:8080/Spring5/textBug.jsp”?
是我運(yùn)行了textBug.jsp文件后獲取的寫明了端口號是8080?
等的路徑。有時(shí)候(比如導(dǎo)入別人的項(xiàng)目,改項(xiàng)目名等的時(shí)候)?
如果報(bào)404,就是要先在WebContent目錄下?
創(chuàng)建一個(gè)jsp文件,這樣的話,運(yùn)行后,再輸入controller的?
訪問路徑后才訪問得了某個(gè)界面,不然的話就算開啟Servers,輸入?
正確的訪問路徑的話也會報(bào)錯(cuò)。





如果報(bào)404的錯(cuò)誤,但代碼沒錯(cuò),那么建議這樣做?
關(guān)于@RequestMapping和return后面的值的設(shè)置,一個(gè)是和訪問路徑有關(guān),一個(gè)是和訪問路徑跳轉(zhuǎn)到的界面的jsp文件名有關(guān)
開啟servers服務(wù)器后瀏覽器輸入訪問的路徑來獲得運(yùn)行效果(這個(gè)是Java Web中通用的運(yùn)行方法,因?yàn)樵贘ava Web中,WEB-INF目錄下創(chuàng)建的jsp文件運(yùn)行后就是會報(bào)404的錯(cuò)誤)。

邊看教程視頻邊做記錄的部分 START


使用在Java Web項(xiàng)目的Spring MVC框架的關(guān)鍵是配置文件的base-package的值,其值代表其控制器類要放的包名






很多都是先從實(shí)現(xiàn)簡單的效果開始,比如打印出”Hello World“,無論學(xué)習(xí)什么框架,vue,微信小程序等等都是先從最簡單的部分開始,實(shí)現(xiàn)后就是開啟了”新世界的大門“,因?yàn)樽铋_始最關(guān)鍵的是環(huán)境的搭建等等。



下面的是更常用關(guān)于Spring MVC框架的web.xml的配置








圖中我框起來的部分的內(nèi)容的意思是:自動把jsp表單提交的內(nèi)容封裝到u這個(gè)User類中,通過打印語句來查看是否封裝成功


個(gè)人理解:Spring MVC框架很”聰明“,當(dāng)你在form表單中的name的賦值和bean中實(shí)體類對應(yīng)的屬性名一致時(shí),提交時(shí)name的賦值中對應(yīng)的value的賦值就會賦值給實(shí)體類對應(yīng)的屬性名。因?yàn)槿绱?,要做注冊等的功能的?shí)現(xiàn)的代碼就會變得更少。



邊看教程視頻邊做記錄的部分 END
個(gè)人的項(xiàng)目經(jīng)驗(yàn) START

?“http://localhost:8080/Spring5/textBug.jsp”
是我運(yùn)行了textBug.jsp文件后獲取的寫明了端口號是8080
等的路徑。有時(shí)候(比如導(dǎo)入別人的項(xiàng)目,改項(xiàng)目名等的時(shí)候)
如果報(bào)404,就是要先在WebContent目錄下
創(chuàng)建一個(gè)jsp文件,這樣的話,運(yùn)行后,再輸入controller的
訪問路徑后才訪問得了某個(gè)界面,不然的話就算開啟Servers,輸入
正確的訪問路徑的話也會報(bào)錯(cuò)。

個(gè)人的項(xiàng)目經(jīng)驗(yàn) END
項(xiàng)目例子記錄 START

package com.SSHC.bean;
public class User {
? ? private String name;
? ? private String sex;
? ? private String edu;
? ? private String hobbys;
? ? private String birth;
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 getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
public String getHobbys() {
return hobbys;
}
public void setHobbys(String hobbys) {
this.hobbys = hobbys;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}

package com.SSHC.controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("test")
public class HelloController {
/**
*?
* 個(gè)人經(jīng)驗(yàn):
?“http://localhost:8080/Spring5/textBug.jsp”
是我運(yùn)行了textBug.jsp文件后獲取的寫明了端口號是8080
等的路徑。有時(shí)候(比如導(dǎo)入別人的項(xiàng)目,改項(xiàng)目名等的時(shí)候)
如果報(bào)404,就是要先在WebContent目錄下
創(chuàng)建一個(gè)jsp文件,這樣的話,運(yùn)行后,再輸入controller的
訪問路徑后才訪問得了某個(gè)界面,不然的話就算開啟Servers,輸入
正確的訪問路徑的話也會報(bào)錯(cuò)。
*/
//http://localhost:8080/Spring5/test/hl
@RequestMapping("hl")
? ? public String hello(){
? ? System.out.println("Hello world");
? ? return "index";
? ? }
//http://localhost:8080/Spring5/test/pn
@RequestMapping("pn")
public String printNow(){
System.out.println(new Date());
return "pub/date";
}
}

package com.SSHC.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.SSHC.bean.User;
@Controller
public class UserController {
//http://localhost:8080/Spring5/addForm
@RequestMapping("addForm")
? ? public String toAdd(){
? ? return "addForm";
? ? }
@RequestMapping(value = "reg",method = RequestMethod.POST)
public String doReg(@ModelAttribute("SpringWeb")User u,ModelMap mm){
//查看輸入的值
System.out.println(u.getName());
System.out.println(u.getsex());
System.out.println(u.getEdu());
System.out.println(u.getHobbys());
System.out.println(u.getBirth());
String msg = "注冊成功!詩書畫唱提醒你該三連啦!";
/*request.setAttribute("_msg",msg);
【以前servlet的傳值的方法,如今在Spring MVC框架中
被put替代*/
mm.put("_msg", msg);
return "info";
}
}

<%@ 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>當(dāng)前日期</h1>
? ? </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">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size: 50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <form action="reg" method="post">
? ? ? ? ? ? <input type="text" name="name" placeholder="請輸入姓名"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="radio" name="sex" value="男"/>男
? ? ? ? ? ? <input type="radio" name="sex" value="女"/>女
? ? ? ? ? ? <br>
? ? ? ? ? ? <select name="edu">
? ? ? ? ? ? ? ? <option value="">請選擇</option>
? ? ? ? ? ? ? ? <option value="本科">本科</option>
? ? ? ? ? ? ? ? <option value="大專">大專</option>
? ? ? ? ? ? ? ? <option value="高中">高中</option>
? ? ? ? ? ? ? ? <option value="初中">初中</option>
? ? ? ? ? ? </select>
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="checkbox" name="hobbys" value="1"/>唱歌
? ? ? ? ? ? <input type="checkbox" name="hobbys" value="2"/>街舞
? ? ? ? ? ? <input type="checkbox" name="hobbys" value="3"/>滑冰
? ? ? ? ? ? <input type="checkbox" name="hobbys" value="4"/>吉他
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="text" name="birth" placeholder="請輸入生日" />
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="submit" value="提交" />
? ? ? ? </form>
? ? </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>Hello world</h1>
? ? </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>${_msg }</h1>
? ? </body>
</html>

<?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-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">? ? ? ? ? ? ? ? ? ? ? ?
? ? <!-- 掃描指定的包和它的子包中的所有java類 -->
? ? <context:component-scan base-package="com.SSHC.controller"/>
? ? <!-- 默認(rèn)注冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter類 -->
? ? <mvc:annotation-driven />
? ? <!-- jsp引用外部js,css等靜態(tài)資源的解決方法(和上面的標(biāo)簽必須同時(shí)出現(xià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)先級設(shè)定 -->
? ? ? ? <property name="order" value="10"></property>
? ? </bean>
</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>Spring5</display-name>
? <!-- controller中文亂碼處理,注意一點(diǎn):要配置在所有過濾器的前面 -->
? <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文件中的。
? ? ? ? ? ? 攔截匹配的請求,Servlet攔截匹配規(guī)則要自已定義,把攔截下來的請求,
? ? ? ? ? ? 依據(jù)相應(yīng)的規(guī)則分發(fā)到目標(biāo)Controller來處理,
? ? ? ? ? ? 是配置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>
開啟servers服務(wù)器后瀏覽器輸入訪問的路徑來獲得運(yùn)行效果(這個(gè)是Java Web中通用的運(yùn)行方法,因?yàn)樵贘ava Web中,WEB-INF目錄下創(chuàng)建的jsp文件運(yùn)行后就是會報(bào)404的錯(cuò)誤)。









