Spring MVC-注解

来源:互联网 发布:淘宝是白号,该怎么升心 编辑:程序博客网 时间:2024/05/17 20:31

基于spring注解的MVC


1.创建web项目

2.引入类库

com.springsource.javax.annotation-1.0.0.jar
com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar

3.在web.xml文件中配置DispatcherServlet

<servlet><servlet-name>action</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>action</servlet-name><url-pattern>/</url-pattern></servlet-mapping> 


4.创建控制器HomeController

public class Person {private String id;private String name;private String address;/** * @return the id */public final String getId() {return id;}/** * @param id the id to set */public final void setId(String id) {System.out.println("正在调用setId方法 ,id=" + id);this.id = id;}/** * @return the name */public final String getName() {return name;}/** * @param name the name to set */public final void setName(String name) {this.name = name;}/** * @return the address */public final String getAddress() {return address;}/** * @param address the address to set */public final void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "{id:"+id+",name:"+name+",address:"+address+"}";}}

@Controller@RequestMapping(value="/home")//根路径public class HomeController {//访问路径 //www.xxx.com/根路径/子路径@RequestMapping(value="/a",method=RequestMethod.GET)//子路径public String method1(@RequestParam(value="id") String uid,String name,String address,Person p,HttpServletRequest req,Map<String, Object> model){//uid = req.getParameter("id");System.out.println("---------method1---------id=" + uid + " name=" + name + " address=" + address);System.out.println("person:" + p);req.setAttribute("msg", "hello world");model.put("key", "value");return "home";//逻辑名}@RequestMapping(value="/b")public String method2(){return "home";}}

5.创建spring配置文件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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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 "><!-- 注解驱动 --><mvc:annotation-driven/><!-- 组件扫描 --><context:component-scan base-package="cn.itcast.springmvc.controller"></context:component-scan><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前缀 /WEB-INF/jsps/home.jsp--><property name="prefix" value="/WEB-INF/jsps/"></property><!-- 后缀 --><property name="suffix" value=".jsp"></property></bean></beans>


6.创建jsp./WEB-INF/jsp/hello.jsp



0 0