spring mvc方法對映器附(案例)
阿新 • • 發佈:2021-11-06
gitee原始碼在springmvc01分支下連結:https://gitee.com/xuxianke/yxyz.git
參考原博主:https://blog.csdn.net/qq_35987774/article/details/79598596
springmvc-servlet.xml檔案,不能使用<mvc:default-servlet-handler>
,會引起請求不被介面卡找到的問題
<!--?xml version="1.0" encoding="UTF-8"?--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <bean id="empController" class="com.xpc.controller.EmpController"> <!--注入方法介面卡--> <property name="methodNameResolver" ref="resolver"></property> <!--注入service--> <property name="empService" ref="empService"> </property></bean> <bean id="empService" class="com.xpc.service.imp.EmpServiceImp"> <!--注入dao--> <property name="empDao" ref="empDao"> </property></bean> <bean id="empDao" class="com.xpc.dao.imp.EmpDaoImp"> <!--注入jdbcTemplate--> <property name="jdbcTemplate" ref="jdbcTemplate"> </property></bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入資料來源--> <property name="dataSource" ref="dataSource"> </property></bean> <!--資料來源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicod=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"> <property name="username" value="root"> <property name="password" value="123456"> </property></property></property></property></bean> <bean id="resolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/emp/login">login</prop> <prop key="/emp/reg">regEmp</prop> <prop key="/emp/findAll">findAll</prop> <prop key="/emp/upd">updEmp</prop> <prop key="/emp/del">delEmp</prop> </props> </property> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <props> <prop key="/emp/**">empController</prop> </props> </property> </bean> <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> </bean></beans>
controller層的類繼承MultiActionController類,methodNameResolver(方法名字介面卡)是這個類中的一個屬性。
package com.xpc.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.xpc.service.EmpService; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; /** * 功能描述:邏輯控制層 * * @author xpc on 2021/11/6 7:54 */ public class EmpController extends MultiActionController { // 注入service private EmpService empService; public void setEmpService(EmpService empService) { this.empService = empService; } public void login(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("aa"); Map query = empService.query(request); JSONObject object = new JSONObject(); object.put("data", query); object.put("code", 0); setResponse(response).getWriter().write(JSON.toJSONString(object)); } public void regEmp(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("aa"); int result = empService.regEmp(request); JSONObject object = new JSONObject(); object.put("data", result + ""); object.put("code", 0); setResponse(response).getWriter().write(JSON.toJSONString(object)); } public void updEmp(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("aa"); int result = empService.updEmp(request); JSONObject object = new JSONObject(); object.put("data", result + ""); object.put("code", 0); setResponse(response).getWriter().write(JSON.toJSONString(object)); } public void delEmp(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("aa"); int result = empService.delEmp(request); JSONObject object = new JSONObject(); object.put("data", result + ""); object.put("code", 0); setResponse(response).getWriter().write(JSON.toJSONString(object)); } public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("aa"); Object result = empService.queryAll(request); setResponse(response).getWriter().write(JSON.toJSONString(result)); } public EmpController() { System.out.println("EmpController 構造"); } /** * 功能描述:設定返回的編碼格式 * * @param response: * @return: javax.servlet.http.HttpServletResponse **/ private HttpServletResponse setResponse(HttpServletResponse response) { response.setCharacterEncoding("utf-8"); return response; } }
web.xml檔案配置
<!--?xml version="1.0" encoding="UTF-8"?--> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--springmvc配置檔案--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.eot</url-pattern> <url-pattern>*.svg</url-pattern> <url-pattern>*.ttf</url-pattern> <url-pattern>*.woff</url-pattern> <url-pattern>*.woff2</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.gif</url-pattern> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/loginEmp.html</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/404.html</location> </error-page> </web-app>
測試:
成功!!
本文來自部落格園,作者:小瓢蟲Study,轉載請註明原文連結:https://www.cnblogs.com/xpcStudy/p/15516394.html