spring MVC開發步驟
- 把spring3提供的jar包拷貝到/WEB-INF/lib/目錄下
- 在web.xml中進行spring3mvc的啟動配置
web.xml放在/WEB-INF/目錄下
配置模版:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
配置spring MVC的核心Servlet控制器
<servlet>
<servlet-name>spring</servlet-name>
<servletclass>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 創建servlet-name-servlet.xml的Action映射文件
說明:
1)該文件應放在/WEB-INF/目錄下
2)servlet-name應為web.xml文件中的<servlet-name>標簽值,本例為spring-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
配置action 采用spring mvc注解的方式把請求映射為相應的action對象中的方法來處理
<!-- 啟用spring mvc 注解 -->
<context:annotation-config />
<!-- 設置使用注解的類所在的jar包 -->
<context:component-scan base-package="com.zte"></context:component-scan>
<!-- 完成請求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
- 編寫spring MVC Action程序
package com.zte.action;
@Controller //類似Struts的Action
public class TestController {
@Resource(name = "loginService")
private LoginService loginService;
@RequestMapping("/login.do")
public String regLogin(User user) {
System.out.println("HelloController.handleRequest()");
loginService.add(user);
return "success";}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。