局部刷新是在前端用ajax做的,和后台没关系
方法/步骤
1.首先把需要的类库导入,整个的结构大概是这样的:
2.建立applicationContext.xml文件,代码如下:
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">
p:location="/WEB-INF/config/jdbc.properties" />
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
3.接下来修改web.xml文件,代码如下:
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">
Servlet定义这里我们定义了请求分发Servlet,即:org.springframework.web.servlet.DispatcherServletDispatcherServlet 是Spring MVC 中负责请求调度的核心引擎,所有的请求将由此Servlet 根据配置分发至各个逻辑处理单元。其内部同时也维护了一个ApplicationContext实例。
-à
请求映射我们将所有以.htm结尾的请求交给Spring MVC进行处理。当然,也可以设为其他值,如.action、.action等。当然用.htm的后缀是不推荐的。在tomcat上会出问题。
-à
4.然后创建dispatcher-servlet.xml内容如下:
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">
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
p:viewName="index" />
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.put("TestFrom", ts);
List
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" %>
<%=request.getLocalAddr() %>
8.编写fail.jsp这个页面是如果登录失败才会跳转到这里:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登陆失败点此返回
9.编写success.jsp文件,如果登录成功,跳转到这个页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="include.jsp" %>
登陆成功!!!!!!!!!!
${item}