用springMVC 返回一个指定的HTML页面的方法:
1、webxml的配置:
<xml version="10" encoding="UTF-8">
<web-app version="25"
xmlns=">
xmlns:xsi=">
xsi:schemaLocation=">
>
<display-name>circcenter</display-name>
<welcome-file-list>
<welcome-file>/admin/loginhtml</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>html</url-pattern>
</servlet-mapping>
<!-- springMVC DispatcherServlet配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>orgspringframeworkwebservletDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>orgspringframeworkwebfilterCharacterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContextxml</param-value>
</context-param>
</web-app>
2、springMVC-servlet文件:
<xml version="10" encoding="UTF-8">
<beans xmlns=">
xmlns:context=">
xmlns:p=">
xmlns:mvc=">
xmlns:xsi=">
xmlns:task=">
xsi:schemaLocation=">
>
>
>
>
>
>
>
<!--<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
-->
<context:component-scan base-package="comqingshuangcirc"/>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀
<bean class="orgspringframeworkwebservletviewInternalResourceViewResolver">
<property name="viewClass" value="orgspringframeworkwebservletviewJstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value="jsp"/>
</bean>
-->
<bean class="orgspringframeworkwebservletviewInternalResourceViewResolver">
<property name="order" value="10"></property>
<property name="prefix" value="/"></property>
<property name="contentType" value="text/html"></property>
</bean>
</beans>
3、controller控制器代码:
@Controller
@RequestMapping(value="/test")
public class TestController {
/
测试请求是否成功
/
@RequestMapping(value="{testAdmin}", method=RequestMethodGET)
public String testRequest(){
Systemoutprintln("----------------请求成功----------------");
return "admin/login<span style="font-family:Helvetica, Tahoma, Arial, sans-serif;">html</span>";
}
4、html目录:
5、运行结果:
获取id ${userid}
获取name ${username}
获取company ${usercompany}
这里举得例子是: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();
}
以上就是关于如何用springMVC 返回一个指定的HTML页面全部的内容,包括:如何用springMVC 返回一个指定的HTML页面、js怎样获得后台Model中的值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)