.net用mvc开发程序,想实现淘宝那种“看了又看”,可以局部刷新的模块。如何实现局部刷新呢?
展开全部
方法/步骤
1.首先把需要的类库导入,整个的结构大概是这样的:
2.建立applicationContext.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: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">
<!-- 属性配置 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/config/jdbc.properties" />
<!--数据源-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
</beans>
3.接下来修改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"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">
<display-name>CMS</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-
Servlet定义这里我们定义了请求分发Servlet,即:org.springframework.web.servlet.DispatcherServletDispatcherServlet 是Spring MVC 中负责请求调度的核心引擎,所有的请求将由此Servlet 根据配置分发至各个逻辑处理单元。其内部同时也维护了一个ApplicationContext实例。
-à
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-
请求映射我们将所有以.htm结尾的请求交给Spring MVC进行处理。当然,也可以设为其他值,如.action、.action等。当然用.htm的后缀是不推荐的。在tomcat上会出问题。
-à
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
</web-app>
4.然后创建dispatcher-servlet.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: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">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- 分发器 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="test.htm">testController</prop>
</props>
</property>
</bean>
<!-- 指定了表现层资源的前缀和后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!--请求/处理单元关系映射-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="testController" class="com.zhkjNaNs.controller.TestController"
p:commandClass="com.zhkjNaNs.controller.TestFrom"
p:fail_view="fail"
p:success_view="success" />
</beans>
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<String, Object> map = new HashMap<String, Object>();
map.put("TestFrom", ts);
List<String> msgList = new LinkedList<String>();
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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getLocalAddr() %>
<form action="test.htm" method="post">
name:<input type="text" name="name"/><br>
pwd:<input type="text" name="pwd"/><br>
<input type="submit">
</form>
</body>
</html>
8.编写fail.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆失败<a href="index.htm">点此</a>返回
</body>
</html>
9.编写success.jsp文件,如果登录成功,跳转到这个页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="include.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功!!!!!!!!!!<br>
<c:out value="${TestFrom.name}"></c:out><br>
<c:forEach items="${msg}" var="item">
${item}<br>
</c:forEach>
</body>
</html>
1.首先把需要的类库导入,整个的结构大概是这样的:
2.建立applicationContext.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: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">
<!-- 属性配置 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/config/jdbc.properties" />
<!--数据源-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
</beans>
3.接下来修改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"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">
<display-name>CMS</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-
Servlet定义这里我们定义了请求分发Servlet,即:org.springframework.web.servlet.DispatcherServletDispatcherServlet 是Spring MVC 中负责请求调度的核心引擎,所有的请求将由此Servlet 根据配置分发至各个逻辑处理单元。其内部同时也维护了一个ApplicationContext实例。
-à
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-
请求映射我们将所有以.htm结尾的请求交给Spring MVC进行处理。当然,也可以设为其他值,如.action、.action等。当然用.htm的后缀是不推荐的。在tomcat上会出问题。
-à
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
</web-app>
4.然后创建dispatcher-servlet.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: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">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- 分发器 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="test.htm">testController</prop>
</props>
</property>
</bean>
<!-- 指定了表现层资源的前缀和后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!--请求/处理单元关系映射-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="testController" class="com.zhkjNaNs.controller.TestController"
p:commandClass="com.zhkjNaNs.controller.TestFrom"
p:fail_view="fail"
p:success_view="success" />
</beans>
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<String, Object> map = new HashMap<String, Object>();
map.put("TestFrom", ts);
List<String> msgList = new LinkedList<String>();
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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getLocalAddr() %>
<form action="test.htm" method="post">
name:<input type="text" name="name"/><br>
pwd:<input type="text" name="pwd"/><br>
<input type="submit">
</form>
</body>
</html>
8.编写fail.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆失败<a href="index.htm">点此</a>返回
</body>
</html>
9.编写success.jsp文件,如果登录成功,跳转到这个页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="include.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功!!!!!!!!!!<br>
<c:out value="${TestFrom.name}"></c:out><br>
<c:forEach items="${msg}" var="item">
${item}<br>
</c:forEach>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询