.net用mvc开发程序,想实现淘宝那种“看了又看”,可以局部刷新的模块。如何实现局部刷新呢?

2026年09月22日 02:39
有2个网友回答
网友(1):

局部刷新是在前端用ajax做的,和后台没关系

网友(2):

方法/步骤

1.首先把需要的类库导入,整个的结构大概是这样的:

2.建立applicationContext.xml文件,代码如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/config/jdbc.properties" />

class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />

3.接下来修改web.xml文件,代码如下:

xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
CMS


default.jsp



contextConfigLocation
/WEB-INF/applicationContext.xml


org.springframework.web.context.ContextLoaderListener

Servlet定义这里我们定义了请求分发Servlet,即:org.springframework.web.servlet.DispatcherServletDispatcherServlet 是Spring MVC 中负责请求调度的核心引擎,所有的请求将由此Servlet 根据配置分发至各个逻辑处理单元。其内部同时也维护了一个ApplicationContext实例。


dispatcher
org.springframework.web.servlet.DispatcherServlet
2

请求映射我们将所有以.htm结尾的请求交给Spring MVC进行处理。当然,也可以设为其他值,如.action、.action等。当然用.htm的后缀是不推荐的。在tomcat上会出问题。


dispatcher
*.htm


30


log4jConfigLocation
/WEB-INF/log4j.properties


4.然后创建dispatcher-servlet.xml内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />


class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">


indexController
testController




class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
p:commandClass="com.zhkjNaNs.controller.TestFrom"
p:fail_view="fail"
p:success_view="success" />


5.接下来写一个pojo:
public class TestFrom {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

6.然后编写TestController.java代码如下:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class TestController extends SimpleFormController{
private String fail_view = null;
private String success_view = null;
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
TestFrom ts = (TestFrom)command;
if (ts.getName().equals("ly") && ts.getPwd().equals("123")){
HashMap map = new HashMap();
map.put("TestFrom", ts);
List msgList = new LinkedList();
msgList.add("这是什么?");
msgList.add("hello world");
msgList.add("你好,Spring MVC");
map.put("msg", msgList);
return new ModelAndView(this.getSuccess_view(),map);
}else{
return new ModelAndView(this.getFail_view());
}

}
public String getFail_view() {
return fail_view;
}
public void setFail_view(String fail_view) {
this.fail_view = fail_view;
}
public String getSuccess_view() {
return success_view;
}
public void setSuccess_view(String success_view) {
this.success_view = success_view;
}

}
7.接下来写index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="include.jsp" %>




Insert title here


<%=request.getLocalAddr() %>


name:

pwd:





8.编写fail.jsp这个页面是如果登录失败才会跳转到这里:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here


登陆失败点此返回


9.编写success.jsp文件,如果登录成功,跳转到这个页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="include.jsp" %>




Insert title here


登陆成功!!!!!!!!!!




${item}