SpringMVC项目案例之---数据的获取与显示

数据的获取与显示

(一)功能
1.对用户输入的数据进行获取

2.将获取的数据显示到页面

3.使用了SpringMVC技术的注解方式

4.使用了过滤器,处理中文乱码问题

5.在web.xml中设置了访问静态资源,保证页面样式的显现

(二)代码实现

1.pom.xml

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>

2.web.xml

<web-app>

  <display-name>Archetype Created Web Application</display-name>

  <!--过滤器,处理中文乱码-->

  <filter>

    <filter-name>encodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>UTF-8</param-value>

    </init-param>

    <init-param>

      <param-name>forceEncoding</param-name>

      <param-value>true</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>encodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!--设置访问静态资源-->

  <servlet-mapping>

    <servlet-name>default</servlet-name>

    <url-pattern>*.css</url-pattern>

  </servlet-mapping>

  <servlet>

    <servlet-name>SpringMVC</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>SpringMVC</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

</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:context="http://www.springframework.org/schema/context"

       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">

<!--将AnnotationHandler自动扫描到IOC容器中-->

    <context:component-scan base-package="handle"></context:component-scan>

    <!--配置视图解析器--><!--将逻辑视图解析成物理视图-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!--配置前缀-->

        <property name="prefix" value="/"></property>

        <!--配置后缀-->

        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

4.User.java

package entity;

public class User {

    private String name;

    private String password;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

}

5.UsreHandler.java

package handle;

import entity.User;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

import org.springframework.web.servlet.ModelAndView;

@Controller

public class UserHandler {

    @RequestMapping("/add")

    public ModelAndView add(User user){

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("user",user);

        modelAndView.setViewName("test");

        return modelAndView;

    }

}

6.index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@page isELIgnored="false" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="add" method="post">

    账户名:<input type="text" name="name">

    密码:<input type="text" name="password">

    <input type="submit" value="登录">

</form>

</body>

</html>

7.test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@page isELIgnored="false" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

用户名:${user.name}

密码:${user.password}

</body>

</html>
上一篇:openstack 问题一览(持续总结中)


下一篇:Node中流的概念