SpringMVC 2 注解方式 创建时间:2016年12月9日(星期五) 晚上6:29 | /WEB-INF/jsp/逻辑视图名,jsp ************************************** web.xml ****************************** <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>welcome</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!--当spring的配置文件在web-inf的目录下 --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:welcome-servlet.xml</param-value> </init-param> --> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ************************************** welcone-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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--这句是默认是xml映射 --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> --> <!--通过bean的name完成url的映射 --> <!-- <bean name="/welcome.html" class="com.gym.controller.Welcome"/> --> <!-- 扫描包下所有定义的注解 --> <context:component-scan base-package="com.gym.controller"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> //前缀 <property name="suffix" value=".jsp"></property> //后缀 </bean> </beans> ***************************** /12-9springMVC/src/com/gym/controller/HelloController.java ****************************** package com.gym.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping(value="/hello") //这是访问的url public String hello(){ System.out.println("gym"); return "welcome"; // 这是逻辑视图名 } } ***************************** web-INF/jsp/welcome.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 'welcome.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> <h1>welcome,springmvc</h1> </body> </html>