--- the acquisition and display of data SpringMVC Projects

Acquisition and display of data

(A) function
1. Get the data entered by the user

2. The obtained data to the page

3. Use annotations way SpringMVC technology

4. Use of a filter, the processing Chinese garbled

5. Set the access static resources in web.xml to ensure the page style show

(B) code for

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 > 

    <! - configuration view resolver -> <! - The logical view resolved to a physical view -> 

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

        < ! - configure the prefix -> 

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

        <-! configuration suffix -> 

        < Property name = "suffix" value . "JSP" = > </ Property > 

    </ the 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>

 

Guess you like

Origin www.cnblogs.com/dyddzhp/p/11316284.html