当前网页的相对路径
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
1.点击页面中链接跳转到后台,经过逻辑处理后跳转前台页面
//前台
<a href="<%=basePath%>user/toLogin">登录</a>
//后台
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 到登录页面
*/
@RequestMapping("/toLogin")
public String toLogin(){
return "user/login";
}
}
//spring-mvc.xml中配置视图解析器可以直接跳转不用带.jsp
<!--试图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
2.单击按钮携带数据跳转到后台处理
//2.1 form表单提交
<form action="<%=basePath%>user/toIndex" method="post">
价格:<input id="min" type="text" name="min" value="${min}"> 元 -
<input id="max" type="text" name="max" value="${max}"> 元
<input type="submit" value="查询" />
</form>
//javaScript的DOM操作[DOM操作](https://www.cnblogs.com/nanchengyuan/p/7191606.html)
<input type="button" value="确定" onclick="window.location.href='<%=basePath%>user/toIndex?cpage='+document.getElementById('pn_input').value+'&min=${min}&max=${max}'">
//2.2 按钮绑定方法
<button onclick="deleteCount(${b.id},${b.count},${b.stock})">-</button>
<script type="text/javascript" src="<%=basePath%>static/js/jquery-1.7.2.js"></script>
<script type="text/javascript">
function deleteCount(id,count,stock){
count = count - 1;
if(count == 0){
alert("再减就没有了哦");
}else{
location.href = "<%=basePath%>manager/changeCarCount?id="+id+"&count="+count;
}
}
}
// 2.3 ajax异步提交
<script type="text/javascript" src="<%=basePath%>static/js/jquery-1.7.2.js"></script>
<script type="text/javascript">
function add(){
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
$.ajax({
url:"<%=basePath%>user/add",
type:"post",
data:{"username":username,"password":password,"email":email},
success:function (obj){
if(obj.success){
alert("添加成功");
location.href = "<%=basePath%>user/list";
}else{
alert("添加失败");
}
},
dataType:"json"
})
}
</script>
<body>
<form>
用户名:<input type="text" id="username" name="username"><br>
密码:<input type="text" id="password" name="password"><br>
邮箱:<input type="text" id="email" name="email"><br>
<button type="button" onclick="add()">添加</button>
</form>
</body>
</script>
3.后台跳转页面
/**
* 到购物车列表页面
*/
@RequestMapping("/toShopCar")
public String toShopCar(Model model,HttpSession session){
List<Car> clist = (List<Car>) session.getAttribute("carList");
Integer totalCount = 0;
BigDecimal totalPrice = new BigDecimal(0);
for (Car car : clist) {
totalPrice = totalPrice.add(car.getTotalPrice());
totalCount += car.getCount();
}
model.addAttribute("totalCount",totalCount);
session.setAttribute("totalCount",totalCount);//作用域在全局
model.addAttribute("totalPrice",totalPrice);//作用域在要跳转的页面
return "cart/cart";
}
/**
* 重定向到别的方法
* 改变购物车中的图书的购买数量
*/
@RequestMapping("/changeCarCount")
public String changeCarCount(Integer id,Integer count,HttpSession session){
List<Car> clist = (List<Car>) session.getAttribute("carList");
for (Car car : clist) {
if(car.getId().equals(id)){
car.setCount(count);
}
}
session.setAttribute("carList",clist);
return "redirect:/manager/toShopCar";//重定向
}