幸会springMVC框架

1、MVC框架所做的事情
1)将url映射到java类的方法
2)封装用户提交的数据
3)处理请求——调用相关的业务处理——封装响应的数据
4)将响应的数据进行渲染,jsp、html、freemark

2、springMVC:轻量级的基于请求响应的mvc框架

3、MVC:效率高、便捷、简单、 和spring无缝集成

二、hello_springMVC
1、web.xml配置分发器

统一代码规范:


<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>



2、添加springMVC配置文件:在web_inf文件下添加[DispatcherServlet name]-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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置handlermapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置handleradapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置渲染器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置请求和处理器 -->
<bean name="/hello.do" class="com.controller.HelloController"></bean>

</beans>


3、
统一代码规范:


package can.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

//注解:控制器
@Controller

public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest req,HttpServletResponse rep) {
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "hello mvc"); //封装要显示到视图的数据
mv.setViewName("hello"); //视图名
return mv;
}

}




三、注解开发springMVC
1、web.xml配置
统一代码规范:

<!-- 代码注释 -->
if(you.hand==cold&amp;&amp;weather;=winter)
giveyoulove(myhand.temp,yourhand.temp)
return you.happyface;
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<display-name>Archetype Created Web Application</display-name>
<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:mvc.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>
</servlet-mapping>
</web-app>

2、在src目录下创建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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置渲染器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图前缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 包扫描 -->
<context:component-scan base-package="can.controller"></context:component-scan>

</beans>


3、HelloController 类
:

<!-- 代码注释 -->
package can.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

//注解:控制器
@Controller

public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest req,HttpServletResponse rep) {
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "hello mvc"); //封装要显示到视图的数据
mv.setViewName("hello"); //视图名
return mv;
}

}



四、配置controller的几种方式
1、为url匹配bean
mvc.xml

<!-- 代码注释 -->
<!-- 映射-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="can.spring.controller"></bean>

2、注解方式:以上

五、跳转结果的方式
1、设置modldandView对象,根据view得名称和视图解析器 跳转到指定页面





ModelAndView mv=new ModelAndView();
mv.addObject("msg","controller");
mv.setViewName("hello");
return mv;
2、根据sevletapi来跳转实现转发,不需要视图渲染器


@Controller
public class controll {
@RequestMapping("/hello")
public void hello(HttpServletRequest req,HttpServletResponse rep) throws IOException {
//rep.getWriter().println("this is controller use servlet api");
rep.sendRedirect("index.jsp");
}

}

3、根据sevletapi来跳转实现转发(带参转发)不需要视图渲染器


@Controller
public class controll {
@RequestMapping("/hello")
public void hello(HttpServletRequest req,HttpServletResponse rep) throws IOException, ServletException {
//rep.getWriter().println("this is controller use servlet api");
//rep.sendRedirect("index.jsp");
req.setAttribute("msg", "hello servlet sendr");
req.getRequestDispatcher("index.jsp").forward(req, rep);
}

}

4、springMVC实现转发1:不需要视图渲染器


@RequestMapping("/hello2")
public String hello() {
return "index.jsp";
}

5、springMVC实现转发2 不需要视图渲染器

@RequestMapping("/hello2")
public String hello() {
//return "forward:index.jsp";
//return "redirect:index.jsp";
}


6、springMVC实现转发2 :需要视图渲染器

@RequestMapping("/hello3")
public String hello3() {
return "hello";
}
7、springMVC实现重定向 :需要视图渲染器

@RequestMapping("/hello3")
public String hello3() {
return "redirect:hello.do";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值