Spring mvc注解方式

  1. 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>springmvc02</display-name>

	<!-- 配置前端控制器 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化时,加载配置文件(处理器映射器、处理器适配器、视图解析器) -->
		<init-param>
			<!-- contextConfigLocation:指定springmvc配置文件的位置 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<!-- servlet容器启动时加载此servlet -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<!-- 第一种方式:*.action,所有url地址是以.action结尾才能被前端控制器进行处理 -->
		<!-- 第二种方式: /,所有的地址都会被前端控制器进行处理,使用此种方式可以实现restful的url-->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>
  1. 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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        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">
	
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.hpe.controller"></context:component-scan>
	
	<!-- 注解的处理器映射器
		 RequestMappingHandlerMapping:对类中标记@RequestMapping注解的方法进行映射
	 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
	
	<!-- 注解的处理器适配器
		 RequestMappingHandlerAdapter:执行handler,对标记@RequestMapping的方法进行适配
	 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
	
	<!-- 注解驱动,会自动注册bean,包括:注解的处理器映射器和处理器适配器 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/user/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>
  1. Controller.java
public class UserController {
	// 查询用户
	// @RequestMapping:定义请求url到处理器功能方法的映射。(配置访问次方法的url)
	// 将方法名和url地址做了个对应;.action可以省略不加
	@RequestMapping("/selectAll")
	public ModelAndView selectAll(){
		List<User> list = new ArrayList<User>();
		list.add(new User(1, "zhangsan", "123", "张三"));
		list.add(new User(2, "lisi", "123", "李四"));
		list.add(new User(3, "wangwu", "123", "王五"));
		// 创建ModelAndView对象
		ModelAndView modelAndView = new ModelAndView();
		// 填充数据
		modelAndView.addObject("userList", list);
		// 设置逻辑视图名称
		modelAndView.setViewName("userList");
		return modelAndView;
	}
	
	// 新增用户
	@RequestMapping(value="/addUser")
	public void addUser(){
		System.out.println("addUser...");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值