码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc 简化Javaweb 简单应用

时间:2017-09-01 18:56:08      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:jar包   ref   pre   默认   color   format   print   网页   数据   

第一步 :

导入外部jar包,放在如图目录下

技术分享

第二步:简单配置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>springmvc01</display-name>
  
  <!-- 配置Spring mvc前端控制器 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 如何不配置初始化参数,会默认加载WEB-INF/前端控制器名-servlet.xml -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- classpath代表应用程序根路径 -->
          <param-value>classpath:app-mvc.xml</param-value>
      </init-param>
      <!--  设置该servlet为web容器启动时立即加载 -->
      <!-- 值正数时,正数值越小,优先级越高,为负数web容器启动时,默认不会立即初始化springmvc-->
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  
  
</web-app>

第三步:配置 spring  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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd">
    
    <!-- 扫描控制器所在的包 -->
    <context:component-scan base-package="com.zhidi.controller"/>
    
    <!-- 开启springmvc注解 -->
    <mvc:annotation-driven/>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置返回视图的前缀 -->
        <property name="prefix" value="/WEB-INF/page/"/>
        <!-- 配置返回视图的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
    
</beans>

第四步:servlet 服务层多个servlet服务器  现在可以简写到controller类中

浏览器进入网页会先进过控制层

package com.zhidi.controller;

import java.util.Date;

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

//会将该类交给Spring IOC容器管理
@Controller
public class HelloController {

    //URI
    @RequestMapping("/helloworld")
    public String hello() {
        //  /WEB-INF/page/success.jsp
        return "success";
    }
    
    @RequestMapping("/test")
    public String test() {
        return "success";
    }
}

配置文件会根据方法返回字符窜自动拼接  如  success  会  进入  success.jsp  网页

表单传输数据简单操作:

建立实体类    属性名和表单  input name 名一样如下

package com.zhidi.entity;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

public class Student {
    
    private Integer id;
    private String stuName;
    private Integer age;
    private String sex;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public Integer getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
    
    
    @Override
    public String toString() {
        return "Student [id=" + id + ", stuName=" + stuName + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday
                + "]";
    }

}




jsp  页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user/edit.do" method="post">
    学生编号:<input type="text" name="id"><br>
    学生姓名:<input type="text" name="stuName"><br>
    年龄:<input type="text" name="age"><br>
    性别:<input type="radio" value="0" name="sex"><input type="radio" value="1" name="sex">女<br>
        生日:<input type="date" name="birthday"><br>
        <input type="submit" value="提交">  
</form> 
</body>
</html>

 

controller类

package com.zhidi.controller;

import java.util.Date;

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

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhidi.entity.Student;

@Controller
@RequestMapping("/user")
public class UserController {
    
    //在RequestMapping中设置属性值,可以匹配更精确的url
    @RequestMapping(value="/add", method=RequestMethod.GET, params="username")
    public String add(String username) {
        System.out.println(username);
        return "user_add";
    }
    
    //向方法中添加默认类型的参数,HttpServletRequest,HttpServletResponse
    @RequestMapping("/defaultParam1")
    public String defaultParam(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("username");
        request.setAttribute("userName", userName);
        HttpSession session = request.getSession();
        return "user_add";
    }
    
    @RequestMapping("/defaultParam2")
    public String defaultParam(HttpSession session) {
        session.setAttribute("userName", "小明");
        return "user_add";
    }
    
    @RequestMapping("/defaultParam3")
    public String defaultParam(Model modelMap) {
        //ModelMap是Model接口的实现类
        //相当于向请求范围中设置了一个参数
        modelMap.addAttribute("userName", "赵日天");
        return "user_add";
    }
    
    //如果请求中的参数名和方法形参中的参数名不一致,可以通过@RequestParam注解将指定名称的参数绑定到对象的形参上
    //@RequestParam的required默认为true,必须传该参数,否则会出现400,可以将其设置为false,就代表请求参数列表中可以没有改参数,使用defaultValue可以为其添加默认值
    @RequestMapping("/simpleParam1")
    public String simpleParam(@RequestParam(value = "name", required = false, defaultValue = "赵特助") String username, Integer id) {
        System.out.println(username);
        System.out.println(id);
        return "user_add";
    }
    
    /**
     * 跳转到user_edit页面
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEdit() {
        return "user_edit";
    }
    
    /**
     * 处理表单提交
     * 属性的自动装箱,会将表单提交的属性值设置到方法中的形参上
     * @param stu
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Student stu) {
        System.out.println(stu);
        return "user_edit";
    }
    
    /**
     * springmvc中不能直接映射时间类型,需要通过其他方式去对时间进行转换,@DateTimeFormat注解,使用该注解时需要开启<mvc:annotation-driver/>
     * @param date
     * @return
     */
    @RequestMapping("/testDate")
    public String testdDate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println(date);
        return "user_edit";
    }
}

 

springmvc 简化Javaweb 简单应用

标签:jar包   ref   pre   默认   color   format   print   网页   数据   

原文地址:http://www.cnblogs.com/static-method/p/7464617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!