基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填參,EL取值)

Posted ljbguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填參,EL取值)相关的知识,希望对你有一定的参考价值。

1、加入jar

技术分享

2、web.xml配置:

<?xml version="1.0" encoding="UTF-8"?

>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <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>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
3、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.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">
  <property name="prefix" value="/WEB-INF/jsp/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>
</beans>

4、实体bean

package cn.itcast.springmvc.domain;

public class User {

 private String name;
 private String address;
 private Integer age;
 private String tel;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  System.out.println("正在通过setName方法注入name的值:" + name);
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public String getTel() {
  return tel;
 }

 public void setTel(String tel) {
  this.tel = tel;
 }

 @Override
 public String toString() {
  return "{name:" + name + ",address:" + address + ",age:" + age
    + ",tel:" + tel + "}";
 }

}

 

5、编写HomeController,代码例如以下:

package cn.itcast.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.springmvc.domain.User;

/**
 * @brief IAccountDao.java 学习Spring注解方式
 * @attention
 * @author 涂作权
 * @date 2014-5-18
 * @note begin modify null
 */
@Controller  //加入注解
@RequestMapping(value = "/home") // 根路径。有些类似strut2的命名空间
public class HomeController {

 /**
  * 子路径,表示仅仅支持get提交
  * @param req 能够通过传递HttpServletRequest的方式获得參数
  * @param name 表示连接的地方有:XXX?name=
  * @param u 假设url的?后面參数过多,要想获得參数。能够直接将这个參数写成User
  * @param model :定义一个Map对象,能够通过这样的方式将之传递给jsp页面
  *
  * @attention url地址能够是:http://localhost:8081/SpringMVC_02/home/hello
  *         ?name=toto&address=haidian&age=24&tel=136XXX
  * 获得的參数为:正在运行hello方法 name:toto User: {name:toto,address:haidian,age:24,tel:136XXX}
  * @return
  */
 @RequestMapping(value="/hello",method=RequestMethod.GET)
 public String hello(HttpServletRequest req,
   @RequestParam(value = "name")
   String name, User u, Map<String, Object> model) {
  //String name = req.getParameter("name");
  System.out.println("正在运行hello方法 name:" + name);
  System.out.println("User: " + u);
  //req.setAttribute("msg", "hello " + name);
  model.put("msg", "hello " + name);
  return "hello";//逻辑名
 }
 
 /**
  * \brief 定义方法hi
  *
  * @return
  * @attention url的地方通过/home/hi的方式訪问要想訪问的地址
  * @author 涂作权
     * @date 2014-5-18
  * @note begin modify by null
  */
 @RequestMapping(value="/hi") //子路径
 public String hi(){
  System.out.println("正在运行hi方法");
  return "hi";  //逻辑名
 }
}

6、编写的hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <title> ‘hello.jsp‘</title>
  </head>
 
  <body>
    ${requestScope.msg}
  </body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 






























































































































以上是关于基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填參,EL取值)的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC系列:(11)返回JSON

Spring基于注解开发异常

Spring4.0学习笔记008——AOP的配置(基于注解)

Spring4.0学习笔记008——AOP的配置(基于注解)

spring AOP (包含基于注解和配置文件两种方式)

Spring MVC系列Spring MVC应用及ModelAndView(模型视图)