在页面上获取controller中的值

这里写图片描述
这里写图片描述
准备工作:
Address.java

package com.lq.vo;

public class Address {

    private String detail;

    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }
    public Address(String detail) {
        super();
        this.detail = detail;
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Insert title here</title>
        <script type="text/javascript" src="./jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            function test(){
                $.ajax({
                    url:"./get",
                    dataType:"json",
                    success:function(data){
                        for(var i=0;i<data.length;i++){
                            console.log(data[i].detail);
                        }
                    }
                });
            }   
        </script>
    </head>
    <body>
         <a href="###" onclick="test()">get</a> 
         <form action="./get" method="post" enctype="multipart/form-data">
            <input name="bookName"/>
            <input type="file" name="file"/>
            <input type="submit" value="提交"/>
        </form>
    </body>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Insert title here</title>
    </head>
    <body>
        ${address.detail}
    </body>
</html>

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"
    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.lq"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>  

    <!-- http://www.cnblogs.com/hujingwei/p/5349983.html -->
    <mvc:default-servlet-handler/>
    <!-- 配置静态资源 -->
    <mvc:resources location="/js" mapping="/*"></mvc:resources>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="102400"></property>
    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>mvc</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- http://jinnianshilongnian.iteye.com/blog/1602617 -->
    <!-- 
        <url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
        <url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等) 
    -->
    <servlet>
        <servlet-name>dispatcherServlet</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值 
            相当于:request.setCharacterEncoding -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!-- forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解, 
            因为这个参数的值只不过是指定response的字符集是否也设置成encoding所指定的字符集, 
            所以你可以选择设置为true或false true:request.setCharacterEncoding(“”); response.setCharacterEncoding(“”); 
            false:request.setCharacterEncoding(“”); (默认为false) -->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

下面看一下controller中的代码

package com.lq.userinfo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.tomcat.jni.File;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.lq.vo.Address;

@Controller
public class UserInfoController {

//  @RequestMapping("/get")
//  public String get(HttpServletRequest request) {
//      Address address = new Address();
//      address.setDetail("山西临汾");
//      request.setAttribute("address", address);
//      return "result";
//  }

//  @RequestMapping("/get")
//  public String get(Map<String,Address> map) {
//      Address address = new Address();
//      address.setDetail("山西临汾");
//      map.put("address", address);
//      return "result";
//  }

//  @RequestMapping("/get")
//  public ModelAndView get() {
//      Address address = new Address();
//      address.setDetail("山西临汾");
//      ModelAndView modelAndView = new ModelAndView("result");
//      modelAndView.addObject("address",address);
//      return modelAndView;
//  }

//  返回json数据    
    @ResponseBody
    @RequestMapping("/get")
    public List<Address> get() {
        List<Address> list = new ArrayList<>();
        list.add(new Address("北京大兴"));
        list.add(new Address("山西临汾"));
        list.add(new Address("安徽六安"));
        System.out.println(list);
        return list;
    }

//  上传文件    
//  @RequestMapping("/get")
//  public String get(String bookName,@RequestParam("file")MultipartFile file) throws IOException {
//      InputStream inputStream = file.getInputStream();
//      System.out.println("bookName:"+bookName);
//      System.out.println("fileSize:"+file.getSize());
//      String fileName = file.getOriginalFilename();
//      String suffix = fileName.substring(fileName.lastIndexOf("."));
//      IOUtils.copy(inputStream, new FileOutputStream("D://"+UUID.randomUUID().toString()+suffix));
//      return "result";
//  }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值