本文还是按照一个最基本的顺序将配置和文件一一罗列出来。而且有注释。很容易看懂。
1 jar包
2 web.xml,配置第一个DispatcherServlet 前置控制器,配置bean文件的位置。
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--查找controller位置的xml文件配置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行-->
<param-value>/WEB-INF/mvc.xml,/WEB-INF/mvc1.xml</param-value>
<param-value>/WEB-INF/mvc*.xml </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3 配置Controller的xml文件
<beans xmlns="http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance" xmlns:p="http://www.springframework/schema/p"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework/schema/mvc"
xsi:schemaLocation="
http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans-3.0.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context-3.0.xsd
http://www.springframework/schema/mvc
http://www.springframework/schema/mvc/spring-mvc-3.0.xsd
">
<!--主动扫描Controller的配置,使得控制器能够扫描到相关的类-->
<context:component-scan base-package="cn.zhang.mvc" />
<!--配置一个返回视图用的bean,下面的这个class整合了jstl技术-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
4 Controller的编写
package cn.zhang.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller 重要的注解。这个才可以扫描到这个类
public class StartAction {
@RequestMapping("/start") //重要的注解,这样才可以到达这个方法
public String start(){
return "start";
}
}
以上为第一次交互,已经完成。最简单的配置
下面开始传递参数的案例:
Spring 3.0的 restful风格
URL temp模板
传参url的差异吗,由于模板的限制
http://localhost:8080/@mvc/start.do?name=zhangsan
http://localhost:8080/@mvc/start/zhangsan.do
在Controller端就需要如下写法来传递参数,@RequestMapping后面写模板,传递多个参数的时候可以像下面这样罗列
@RequestMapping("/start/{name}/{age}")
//从路径中取得参数的方法
/*
* 注意name和路径中的name必须一致才可以,定义模板和定义PathVariable
* 用release模式编译需要这样写@PathVariable("name")
* debug模式只需要@PathVariable String name 这样写即可
*/
方法中获取的话必须按照下面的方法写
public String start(@PathVariable("name") String name, @PathVariable("age") int age){
System.out.println(name);
return "start";
}
更多推荐
Spring mvc (六) [基于注解的spring3 mvc的基本配置和交互原理][注解模板传参]
发布评论