SpringMVC
1.使用注解编写Controller类
①每个请求处理参数对应一个URL
@Controller
public class BbsForumController{
@Autowired
private BbsForumService bbsForumService;
@RequestMapping(“/listAllBoard.do”)
public String listAllBoard(){
bbsForumService.getAllBoard();
System.out.println(“call listAllBoard method”);
return “llistBoard”;
}
@RequestMapping(“/listBoardTopic.do”)
public String listBoardTopic(int topicId){//该处的topicId需要通过url传递
bbsForumService.getBoardTopics(topicId);
System.out.println(“call listBoardTopic method”);
return “listTopic”;
}
}
注:/listAllBoard.do的url请求将由listAllBoard()方法处理,而/listBoardTopic.do?
topicId=1方法将由listBoardTopic()处理
②一个Controller对应一个url,由请求参数决定请求处理方法
@Controller
@RequestMapping(“/bbsForum.do”)
public class BbsForumController{
@Autowired
private BbsForumService bbsForumService;
//如果URL请求中包含“method=listAllBoard”的参数,有本方法进行处理
@RequestMapping(params=“method=listAllBoard”)
public String listAllBoard(){
bbsForumService.getAllBoard();
System.out.println(“call listAllBoard method”);
return “llistBoard”;
}
//如果URL请求中包括“method= listBoardTopic”的参数,有本方法进行处理
@RequestMapping(params=“method=listBoardTopic”)
public String listBoardTopic(int topicId){//该处的topicId需要通过url传递
bbsForumService.getBoardTopics(topicId);
System.out.println(“call listBoardTopic method”);
return “listTopic”;
}
}
③让请求处理方法处理特定HTTP请求方法
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
@RequestMapping(params = "method=createTopic",method = RequestMethod.POST)
public String createTopic(){
System.out.println("call createTopic method.");
return "createTopic";
}
}
注:只有当/bbtForum.do?method=createTopic请求以post方法提交时,createTopic()方法才会进行处理
2.springMVC配置文件解析:
A.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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
a.DispatcherServlet为控制器,拦截匹配的请求,这里定义为所有的请求,把拦截到的请求依据相应的规则分发到对应的Controller中
b.contextConfigLocation加载spring-servlet.xml详细的配置文件
c.load-on-startup定义启动顺序
B.spring-servlet.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-3.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-3.0.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 扫描controller控制器包,否则DispatcherServlet无法找到相应的controller --> <context:component-scan base-package="com.tang.controller.annotation"></context:component-scan> <mvc:annotation-driven/> <!-- 启动springmvc的注解功能,完成请求和注解pojo的映射 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> --> <!-- 另外最好加入DefaultAnnotationHandlerMapping,不然会被xml或其他的映射覆盖 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> --> <!-- 引入多视图控制 --> <!-- <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"></property> </bean> --> <!-- 静态资源访问 --> <!-- <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> --> <!-- 改语句可以访问所有的静态资源 --> <mvc:default-servlet-handler /> <!-- 视图分解器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>