标签:
1.导入jar包
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.编写spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.bdqn"/>
<!-- 视图解析 器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4.编写Controller类
package com.bdqn.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String sss(){
System.out.println("helloWorld");
return "success";
}
}
5.编写页面.jsp
<body>
<a href="helloworld">helloworld</a>
</body>
@RequestMaping注解不但可以注解方法还可以注解类
@RequestMapping(values=””,method=”post”)
可以使用params和headers来更加精确映射条件,params和headers
@RequestMapping(values=””,param=”{}”,headers=”{}”);
在web.xml里面配置org.springframwork.web.filter.hiddenHttpMethodFilter可以把post请求转换为其他的
Map和ModelMap处理模型数据
@RequestMapping(“/testMap”)
Public String testMap(Map<String,object> map){
Map.put(“names”,array.aslist(“tome”));
System.out.println(map.);
}
@RequestMapping(“/testSessionAttributes”)
Public String testSessionAttributes(Map<String,object> map){
User user=new User();
Map.put(“user”,”user”);
Return success;
}
标签:
原文地址:http://www.cnblogs.com/tgmo/p/4800601.html