Spring配置如下
<!-- Spring mvc 靜態資源處理 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<!-- 配置控制器 -->
<context:component-scan base-package="com.jrtfurniture.www.action"/>
<!-- 配置SpringMVC的視圖解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
表單jsp文件如下
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%--<%@ taglib uri="/struts-tags" prefix="s" %>
--%><!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>橫向菜單設置</title>
</head>
<body>
<form action="write/writeMainTitle" method="get">
菜單1:<input type="text" name='main1' value="${mainTitle1}"><br/>
菜單2:<input type="text" name='main2' value="${mainTitle2}"><br/>
菜單3:<input type="text" name='main3' value="${mainTitle3}"><br/>
菜單4:<input type="text" name='main4' value="${mainTitle4}"><br/>
菜單5:<input type="text" name='main5' value="${mainTitle5}"><br/>
菜單6:<input type="text" name='main6' value="${mainTitle6}"><br/>
菜單7:<input type="text" name='main7' value="${mainTitle7}"><br/>
<input type='submit' value='提交'/>
<input type='reset' value='重置'/>
</form>
<a href='/jrt/back/main.jsp'>返回</a>
</body>
</html>
這個jsp是從action跳轉過來的,action的代碼爲
@Controller
@RequestMapping("/read")
public class ReadXmlAction {
@RequestMapping("/readMainTitle")
public String readMainTitle(HttpServletRequest request)throws Exception{
mainTitles = MyXmlUtil.read(Const.TITLE_FILE_PATH+Const.MAIN_FILE_NAME, Const.TAG_MAIN);
int i = 0;
for(String m:mainTitles){
i++;
request.setAttribute("mainTitle"+i, m);
System.out.println("mainTitle"+i+"="+m);
}
return "back/menu/mainTitle";
}
}
提交表單出現問題
url變成了:http://localhost:8989/jrt/read/write/writeMainTitle,即Spring自動添加地址的前綴爲read
在jsp中添加代碼
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
後又跳轉,這時出現重複頁面,如圖