1.构建web的maven工程(具体看idea的web构建篇:
idea的web构建篇
):
2.直接建立类,一部到位:
2.一个简单的springmvc例子:
HelloContorller.java
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
System.out.println("hello StringMvc");
return "success";
}
springmvc.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">
<!--1.开启注解扫描,相当于把它交给ispringoc容器-->
<context:component-scan base-package="cn.xiaosi.controller"></context:component-scan>
<!--2.配置视图解析器解析类
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
System.out.println("hello StringMvc");
return "success";
}
-->
<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>
<!--3.开启动springmvc框架注解支持-->
<mvc:annotation-driven/>
</beans>
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>
<!--一启动就加载配置文件启动sprigmvc.xml的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.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>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3 >sprintmvc入门</h3>
<a href="hello">aaaa</a>
</body>
</html>
success.html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
成功了
</body>
</html>