最近在學springMVC,把web和springMVC放在一起看看
剛剛入門寫的不太好
WEB
先看看web的方式吧
JSP頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>nihao</h1>
<a href="hi" target="_blank" rel="external nofollow" >111</a>
</body>
</html>
個人感覺xml配置方式更清楚一些,雖然不夠簡潔
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>gx.controller.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
</web-app>
對應的有一個servlet
Servlet
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 重定向
// response.setStatus(302);
// response.setHeader("location","/WebDome_1/ok.jsp");
// 簡單的轉跳
response.sendRedirect("/WebDome_1/ok.jsp");
}
}
webApp目錄下如果有一個ok.jsp ? 轉跳 : 報錯
在接觸SpringMVC前,感覺不是太複雜
SpringMVC
springMVC今晚剛剛入門
jsp頁面還是那個
web.XML
配置檔案得改一下
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<!--前端控制器-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化參數-->
<init-param>
<!--spring配置檔案的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</init-param>
<!--配置為伺服器啟動時加載-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--所有路徑-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring的配置檔案
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟注解配置-->
<context:component-scan base-package="gx.*"></context:component-scan>
<!--配置視圖解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--轉跳的目錄-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--轉跳的檔案字尾-->
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driver></mvc:annotation-driver>
</beans>
servlet類
@Controller
public class servlet {
@RequestMapping(path = {"/hi"})
public String ruMen(){
System.out.println("入門執行");
return "ok";
}
}
完了?
對, 完了.
但是咱們部落格還沒完, 現在介紹下之前寫的代碼裡的一些東西
配置
web.XML配置裡的前端控制器,springMVC是分子產品的,每個子產品的角色劃分很清晰,
前端控制器
就是其中的一個
初始化參數:讓tomcat加載springMVC的配置檔案,使springmvc的配置生效
<mvc:annotation-driver></mvc:annotation-driver>
在 SpringMVC 的各個元件中,處理器映射器、處理器擴充卡、視圖解析器稱為 SpringMVC 的三大元件。
使 用 mvc:annotation-driven 自動加載 RequestMappingHandlerMapping (處理映射器) 和
RequestMappingHandlerAdapter ( 處 理 适 配 器 ) , 可 用 在 SpringMVC.xml 配 置 文 件 中 使 用
mvc:annotation-driven替代注解處理器和擴充卡的配置。
@RequestMapping注解:
- 作用 : 用于建立請求 URL 和處理請求方法之間的對應關系
-
類上:
請求 URL 的第一級通路目錄。此處不寫的話,就相當于應用的根目錄。寫的話需要以/開頭。
它出現的目的是為了使我們的 URL 可以按照子產品化管理:
例如:
賬戶子產品:
/account/add
/account/update
/account/delete
…
訂單子產品:
/order/add
/order/update
/order/delete
- 方法上: 請求 URL 的第二級通路目錄。
-
- 屬性:
- value:用于指定請求的 URL。它和 path 屬性的作用是一樣的。
- method:用于指定請求的方式。
-
params:用于指定限制請求參數的條件。它支援簡單的表達式。要求請求參數的 key 和 value 必須和 配置的一模一樣。
例如:
params = {“accountName”},表示請求參數必須有 accountName
params = {“moeny!100”},表示請求參數中 money 不能是 100。
- headers:用于指定限制請求消息頭的條件。