Java中ModelAndView是做什么的

Java中ModelAndView是做什么的,第1张

ModelAndView以orgspringframeworkuiModelMap的形式来保持模型数据,通过构造方法传入的或者通过实例方法添加的模型数据都将添加到这个ModelMap中。至于ModelMap中保持的模型数据将会在视图渲染阶段,由具体的View实现类来获取并使用。

我们需要为添加到ModelAndView的一组或者多组模型数据提供相应的键(Key),以便具体的View实现类可以根据这些键获取具体的模型数据,然后公开给视图模板。通常,模型中的数据对应的键需要与视图模板中的标志符相对应,如图所示:

基于JSP/JSTL模板的视图实现,通常是将模型数据通过>

但不管什么视图类型,对应的视图模板都将可以通过添加到ModelAndView的模型数据的键来获取模型数据,并合并到最终的视图输出结果中。

    :

ModelAndView  的构造方法有7个。但是它们都是相通的。这里使用无参构造函数来举例说明如何构造ModelAndView  实例。

ModelAndView 类别就如其名称所示,是代表了MVC Web程序中Model与View的对象,不过它只是方便您一次返回这两个对象的holder,Model与View两者仍是分离的概念。

最简单的ModelAndView是持有View的名称返回,之后View名称被view resolver,也就是实作orgspringframeworkwebservletView接口的实例解析,例如 InternalResourceView或JstlView等等。

ModelAndView(String viewName)  如果您要返回Model对象,则可以使用Map来收集这些Model对象,然后设定给ModelAndView,使用下面这个版本的 ModelAndView: ModelAndView(String viewName, Map model)  Map对象中设定好key与value值。

之后可以在视图中取出,如果您只是要返回一个Model对象,则可以使用下面这个 ModelAndView版本: ModelAndView(String viewName, String modelName, Object modelObject)  藉由modelName,您可以在视图中取出Model并显示。

webservletView的实例,View接口如下: public interface View { public void render(Map model, >

View的实作之前用过 orgspringframeworkwebservletviewInternalResourceView,另外也还有JstlView、 TilesView、VelocityView等等的实作,分别进行不同的表现展处理 。

ModelAndView()  这个构造方法构造出来的ModelAndView  不能直接使用,应为它没有指定view,也没有绑定对应的model对象。当然,model对象不是必须的,但是view确实必须的。  用这个构造方法构造的实例主要用来在以后往其中加view设置和model对象。

给ModelAndView  实例设置view的方法有两  个:setViewName(String viewName) 和 setView(View view)。前者是使用view  name,后者是使用预先构造好的View对象。

其中前者比较常用。事实上View是一个接口,而不是一个可以构造的具体类,我们只能通过其他途径来获取  View的实例。对于view  name,它既可以是jsp的名字,也可以是tiles定义的名字,取决于使用的ViewNameResolver如何理解这个view name。

如何获取View的实例以后再研究。  而对应如何给ModelAndView  实例设置model则比较复杂。有三个方法可以使用:  addObject(Object modelObject)  addObject(String modelName, Object modelObject)  addAllObjects(Map modelMap)  ModelAndView  可以接收Object类型的对象,ModelAndView  将它视为其众多model中的一个。

当使用Object类型的对象的时候,必须指定一个名字。ModelAndView  也可以接收没有明显名字的对象,原因在于ModelAndView  将调用spring自己定义的Conventions 类的getVariableName()方法来为这个model生成一个名字。

获取id   ${userid}

获取name ${username}

获取company ${usercompany}

JS中直接从java后台获得对象的值(数组的值)

这里举得例子是:JS直接从后台Contorller中(SpringMVC中的model中)获得数值的值

Contorller 此处将 talentIntegralRecordsDay talentIntegralRecordsIntegral 两个数组用JSONtoJSONString()封装。

@SuppressWarnings("deprecation")

@RequestMapping("/integralParadise")    public ModelAndView Welcome(>

Users user = userServicecurrentUser(request);  

usergetTalentUser()getIntegral();

Systemoutprintln(usergetTime()getDate());

TalentIntegralRecord[] signInTalentIntegralRecords= wechatIntegralServicegetUserSignInTalentIntegralRecords(usergetId());        int size = signInTalentIntegralRecordslength;        int[] talentIntegralRecordsDay = new int[size];

Long[] talentIntegralRecordsIntegral = new Long[size];        for(int i=0;i<signInTalentIntegralRecordslength;i++){

talentIntegralRecordsDay[i]=signInTalentIntegralRecords[i]getOperatorTime()getDate();

talentIntegralRecordsIntegral[i]=signInTalentIntegralRecords[i]getIntegral();

}

Map<String,Object> map = new HashMap<String,Object>();

mapput("talentIntegralRecordsDay", JSONtoJSONString(talentIntegralRecordsDay));

mapput("talentIntegralRecordsIntegral", JSONtoJSONString(talentIntegralRecordsIntegral));        return new ModelAndView("wechat/integralParadise/rili",map);

}

前台JSP 因为用到两个数组数据的JS代码为页面引用的JS代码所以要在页面中先声明获得后台两个数组(这段JS代码应在引用的JS文件前面)

<script type="text/javascript">

$(document)ready(function(){

windowtalentIntegralRecordsDay = ${talentIntegralRecordsDay};

windowtalentIntegralRecordsIntegral = ${talentIntegralRecordsIntegral};

});</script>

引用的JS文件 开始就获得了两个数组的值

$(function() {    var signFun = function() {      

var dateArray = windowtalentIntegralRecordsDay;// 假设已经签到的

var talentIntegralRecordsIntegral = windowtalentIntegralRecordsIntegral;        var $dateBox = $("#js-qiandao-list"),

$currentDate = $("current-date"),

$qiandaoBnt = $("#js-just-qiandao"),

_html = '',

_handle = true,

myDate = new Date();

$currentDatetext(myDategetFullYear() + '年' + parseInt(myDategetMonth() + 1) + '月' + myDategetDate() + '日');        var monthFirst = new Date(myDategetFullYear(), parseInt(myDategetMonth()), 1)getDay();        var d = new Date(myDategetFullYear(), parseInt(myDategetMonth() + 1), 0);        var totalDay = dgetDate(); //获取当前月的天数

for (var i = 0; i < 42; i++) {

_html += ' <li><div class="qiandao-icon"></div></li>'

}

$dateBoxhtml(_html) //生成日历网格

var $dateLi = $dateBoxfind("li");        for (var i = 0; i < totalDay; i++) {

$dateLieq(i + monthFirst)addClass("date" + parseInt(i + 1));            for (var j = 0; j < dateArraylength; j++) {                if (i == dateArray[j]) {

$dateLieq(i)addClass("qiandao");                    var integral;                    if(talentIntegralRecordsIntegral[j]==0){

integral="大转盘";

}else{

integral="+"+talentIntegralRecordsIntegral[j];

}

$dateLieq(i)find("div")text(integral);

}

}

} //生成当月的日历且含已签到

$("date" + myDategetDate())addClass('able-qiandao');

$dateBoxon("click", "li", function() {                if ($(this)hasClass('able-qiandao') && _handle) {

$(this)addClass('qiandao');

qiandaoFun();

}

@RequestMapping(value = "/login", method = RequestMethodGET)\x0d\public ModelAndView myMethod(>

在控制层中,可以通过以下两种方法获取视图层select标签选中的值:

通过表单提交获取:

在前端页面中,将选中的值作为表单的一个参数,通过form表单提交到后台控制层。在后台控制层中,通过requestgetParameter()方法获取表单参数的值。

示例代码如下:

HTML代码:

```

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

<select name="selectValue">

<option value="value1">选项1</option>

<option value="value2">选项2</option>

<option value="value3">选项3</option>

</select>

<input type="submit" value="提交"/>

</form>

```

控制层Java代码:

```

@RequestMapping("/testController")

public ModelAndView testController(>

String selectValue = requestgetParameter("selectValue");

// do something with selectValue

return new ModelAndView("viewName");

}

```

2 通过AJAX请求获取:

使用ajax请求,在前端页面中获取select标签选中的值,并通过ajax发送到后台控制层。在后台控制层中,通过@RequestParam注解获取ajax请求中的参数值。

示例代码如下:

HTML代码:

```

<select id="selectValue">

<option value="value1">选项1</option>

<option value="value2">选项2</option>

<option value="value3">选项3</option>

</select>

<button onclick="getValue()">获取选中值</button>

<script>

function getValue() {

var selectValue = documentgetElementById("selectValue")value;

$ajax({

type: "POST",

url: "testController",

data: {"selectValue": selectValue},

success:function(data){

// do something with data

}

});

}

</script>

```

控制层Java代码:

```

@RequestMapping("/testController")

public ModelAndView testController(@RequestParam String selectValue) {

// do something with selectValue

return new ModelAndView("viewName");

}

```

以上就是关于Java中ModelAndView是做什么的全部的内容,包括:Java中ModelAndView是做什么的、js怎样获得后台Model中的值、在springMVC中的Controller层中怎么获取页面中的input中的用户输入的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9341655.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存