当前位置:首页 » JavaScript技术

Spring的MVC配置详解

2010-08-12 15:54 本站整理 浏览(5)
web.xml文件内容
-------------------------------web.xml文件内容开始-----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>feresh</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/map-*.xml,
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>feresh</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>180</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----------------web.xml文件内容结束--------------------------
map-test.xml文件内容
----------------------map-test.xml文件内容开始---------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="urlMapping_test01" //在有多个xml文件时一定是唯一的
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test/test01/*">test01</prop>
</props>
</property>
</bean>
<bean id="test01" class="cn.org.ctrl.test01Ctrl">
<property name="methodNameResolver">
<ref bean="InternalPathMethodNameResolver_test01"/>
</property>
</bean>
<bean id="InternalPathMethodNameResolver_test01"
class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/>
</beans>
--------------------------map-test.xml文件内容结束-----------------------
而此时的控制器类一定要extends MultiActionController //继承这个类、是Spring里的一个类
控制器类的方法
public ModelAndView Test(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
request.setCharacterEncoding("utf-8"); //最好加上
response.setCharacterEncoding("utf-8"); //最好加上
Map map = new HashMap();
map.put("KEY1","VALUE1");
map.put("KEY2","VALUE2");
......
可以
return new ModelAndView("/test/test.jsp","result",map);//此时页面应写成${result.KEY1}
也可以
return new ModelAndView("/test/test.jsp",map);//此时页面应写成${KEY1}
也可以如下面这样 //这是采用ajax局部更新常用的
String value="测试";
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println(value);
out.close();
return null;
}
当然控制类方法也可以写成这样
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//调用blh层方法
response.sendRedirect("test2.do");
}
或者
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException{
//调用blh层方法
PrintWriter out = response.getWriter();
out.print("<script Language='JavaScript'>");
out.print("window.opener.location.href='../Test.do?casetype="+casetype+"&caseid="+caseid+"';");
out.print("window.close();");
out.print("</script>");
}