当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
SpringMvc入门教程
生活随笔
收集整理的这篇文章主要介绍了
SpringMvc入门教程
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1.新建demo4 web项目, 导入spring包(使用的是spring4.2)
2.修改WEB-INF下的WEB.XML内容为
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet> <servlet-name>dispatch</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> View Code3.在WEB-INF下的新建spring-servlet.xml,内容为
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><context:component-scan base-package="com"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans> View Code4.在SRC下新建 包 com.game.controller
5.在该包下新建类
package com.game.controller;import java.io.IOException; import java.util.Date;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;@Controller public class Helloworld {@RequestMapping("/helloworlda") //此处控制浏览器里访问路径 具体为:/SpringDemo/helloworldpublic void helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {//输出字符串response.getWriter().append("hello world--a");}@RequestMapping("home") public ModelAndView home(){ ModelAndView mv=new ModelAndView();mv.addObject("name","xiaoxiao");mv.getModel().put("age", "111110");return mv;} } View Code6.在web-inf下新建名为jsp的文件夹,并新建home.jsP文件,内容为
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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 'home.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>This is my JSP page. ${age} ==${name} }<br></body> </html> View Code7.打开地址 http://localhost:8080/demo4/home.do
总结
以上是生活随笔为你收集整理的SpringMvc入门教程的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 详细讲解-sphinx配置文件
- 下一篇: 倍逆序式