springmvc入门

springframework
springmvc
srpingboot

mvc 框架
Model 模型层 模型bean类
View 视图层 jsp el jstl
Controller 控制器层 servlet

java mvc框架 必须web项目
1) struts1.x

2) struts2.x struts+webwork 新框架 拦截器(过滤器)

3) springmvc
servlet

springmvc入门
1、建立maven项目的web项目

2、pom.xml文件四入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

3、修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>smvc01</display-name>

<!-- springmvc servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 会自动加载/WEB-INF/springmvc-servlet.xml springmvc框架配置文件 -->
<!-- 如下配置,会自动加载src/main/resources/springmvc*.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>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- welcome home -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、
在resources/建立springmvc.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">

<!-- HandlerMapping HelloController 可以直接hello.do hello.action 根据类名来查找映射 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!-- 配置handerAdapter 适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

<!-- 处理映射器 根据bean name 名称进行查找 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!-- 配置视图渲染器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 将视图名 渲染后视图的前缀 -->
<property name="prefix" value="/"/>
<!-- 渲染后视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>

<!-- 简单处理映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.action">a</prop>
<prop key="/b.action">a</prop>
<prop key="/c.action">a</prop>
</props>
</property>
</bean>

<!-- 控制器 处理器 -->
<!-- test.do test.action user.action a.action b.action c.action-->
<bean class="com.fz.controller.TestController" id="a" name="/t.action"/>
<bean class="com.fz.controller.Hello"/>

<!-- 引入其它配置文件 -->
<import resource="my.xml"/>
</beans>
在resources/建立my.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">


<bean class="com.fz.controller.UserController" name="/uu.action"/>
</beans>
5、编写控制器类
src/main/java/com/fz.controller/TestController.java
package com.fz.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-24.
*/
public class TestController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("<h3>Test-SpringMVC-world--中文效果</h3>");
out.flush();
out.close();
System.out.println("hello-test");
return null;
}
}


package com.fz.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
* Created by webrx on 2017-06-24.
*/
public class Hello implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("<h3>Hello123</h3>");
out.flush();
out.close();
System.out.println("hello-hello");
return null;
}
}


package com.fz.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Created by webrx on 2017-06-24.
*/
public class UserController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
ModelAndView mv = new ModelAndView("user");// 项目根目录下 /user.jsp
mv.addObject("name","张三丰");
mv.addObject("addr",new String[]{"郑州","北京","天津"});
mv.setViewName("WEB-INF/show"); //找/WEB-INF/show.jsp 渲染 /WEB-INF/show.jsp
return mv;
}
}


6、部署项目测试

http://localhost/test.do
http://localhost/user.do
http://localhost/uu.action

posted @ 2017-07-07 10:51  归然  阅读(133)  评论(0编辑  收藏  举报