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

1、添加jar

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

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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:linux命令行介绍及使用(一)


下一篇:linux下使用crontab无法启动tomcat服务问题解决办法