當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springmvc入门案例(1)
生活随笔
收集整理的這篇文章主要介紹了
Springmvc入门案例(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
據說,現在springmvc火了,好多企業都在使用,既然這樣,咱們也得會點,于是乎就開始自學了,通過找資料,終于做出來了一個簡單案例,這里分享供大家瀏覽,主要分為以下幾個步驟:
一、springmvc是一個mvc框架,通過mvc很好的將數據、業務、表現進行分離
二、springmvc是圍繞DispatcherServlet展開的,由DispatcherServlet負責將請求派發到特定的handler
springmvc框架搭建步驟:
?? ?1.創建項目,拷貝springmvc相關的jar包。
?? ?2.配置web.xml主要配置前端控制器DispatcherServlet
?? ???
?? ?3.在web-inf下創建springmvc配置文件,[servletname]-servlet.xml,文件名必須是springmvc-servlet.xml
?? ?4.配置HandlerMapping映射,根據beanname找到對應的Controller(可以省略)
?? ??? <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping">
?? ?5.創建jsp頁面,需要請求發出請求的頁面
?? ?6.創建Controller,繼承AbstractController,重寫handleRequestInternal(HttpServletRequest request,
?? ??? ??? ?HttpServletResponse response)
?? ??? //返回數據和頁面@Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {String hello = request.getParameter("file");System.out.println(hello);ModelAndView modelAndView = new ModelAndView("welcome"); //返回到的頁面modelAndView.addObject("helloword",hello); //返回值return modelAndView;}
?? ?7.配置視圖解析器
?? <!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置前綴和后綴 --><property name="prefix" value="/"></property><!-- 后綴 --><property name="suffix" value=".jsp"></property></bean>
下面我把源碼放上來吧;
??? web.xml里面的配置:
?? ??? ?
Springmvc里面的配置:
<?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"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 配置HandlerMapping映射,根據beanname找到對應的Controller --><bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean><!-- 配置Controller --><bean id="hello.do" class="org.controller.HelloController"></bean><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置前綴和后綴 --><property name="prefix" value="/"></property><!-- 后綴 --><property name="suffix" value=".jsp"></property></bean> </beans>
??? HelloController控制器里面的代碼:
/** * @Title: HelloController.java* @Package org.controller* @Description: TODO該方法的主要作用:* @author A18ccms A18ccms_gmail_com * @date 2017-8-30 下午8:38:53* @version V1.0 */package org.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;/** * * 項目名稱:springmvc_day1 * 類名稱:HelloController * 類描述: 控制器* 創建人:Mu Xiongxiong * 創建時間:2017-8-30 下午8:38:54 * 修改人:Mu Xiongxiong * 修改時間:2017-8-30 下午8:38:54 * 修改備注: * @version * */public class HelloController extends AbstractController {@Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {String hello = request.getParameter("file");System.out.println(hello);ModelAndView modelAndView = new ModelAndView("/welcome"); //返回到的頁面modelAndView.addObject("helloword",hello); //返回值return modelAndView;}}
index.jsp里面的代碼:
<%@ page language="java" import="java.util.*" 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 href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</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"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="hello.do" method="post">hello:<input type="text" name="file"/><br/><input type="submit" value="提交"/></form></body> </html>
welcome.jsp的代碼:
<%@ page language="java" import="java.util.*" 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 href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</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"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>hello:${helloword }</body> </html>
最后看一下目錄對應關系:
運行看welcome.jsp頁面:
運行完畢,遇到的問題:
Springmvc中提交from之后不跳轉不進控制器
?Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解決方案】
總結
以上是生活随笔為你收集整理的Springmvc入门案例(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白毛女的故事简介 白毛女的故事介绍
- 下一篇: WRF文件打开方式