大家好,又见面了,我是你们的朋友全栈君。
SSM项目总结(基于Maven工程)
1、如何访问WEB-INF下的页面
<% request.getRequestDispatcher("/WEB-INF/views/home/login1.jsp").forward(request, response); %>
2、session
将登录用户信息存在session中
session.setAttribute("account_session", account);
将session中数据销毁
session.removeAttribute("account_session");
获取session中的数据
${
sessionScope.account_session.user.username}
3、AJAX
AJAX内跳转页面
window.location.href = "index"
4、数据提交
form表单
<form action="index.html">
</form>
ajax
$.ajax({
url: "insertUser",
type: "post",
dataType: "json",
data:{
"username":add_username,
}
});
5、onclick 事件
<script type="text/javascript">
function save(){
if(confirm('是否确认?')){
$.ajax({
url: "insertUser",
type: "post",
dataType: "json",
data:{
"username":add_username,
},
success:function(result){
if(result.code == 100){
alert("成功!!!");
} else {
alert("失败");
}
}
});
}
}
</script>
6、html
下拉列表
<select id = "add_sex" >
<option value ="1">男</option>
<option value ="2">女</option>
</select>
7、拦截器
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Account account = (Account) request.getSession().getAttribute("account_session");
if(account!=null) {
// 登录成功不拦截
return true;
}else {
// 拦截后进入登录页面
request.getRequestDispatcher("/WEB-INF/views/home/login.jsp").forward(request, response);
return false;
}
}
dispatcherServlet-servlet.xml文件
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/home/index" />
<mvc:exclude-mapping path="/indexSecond" />
<mvc:exclude-mapping path="/static/**" />
<bean class="com.lv.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
8、c:forEach
// 查询所有用户
@RequestMapping(value ="/AccountList",method = RequestMethod.GET)
public String getAccountsList(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model) {
PageHelper.startPage(pn,5);
//startPage后面紧跟这个查询就是一个分页查询
List<Account> list = accountService.getAll();
//使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就可以了
PageInfo page = new PageInfo(list,5);
model.addAttribute("pageInfo", page);
return "account/list";
}
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${pageInfo.list}" var="account">
<tr>
<td>${
account.id}</td>
</tr>
</c:forEach>
9、分页查询
// 查询所有用户
@RequestMapping(value ="/AccountList",method = RequestMethod.GET)
public String getAccountsList(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model) {
PageHelper.startPage(pn,5);
//startPage后面紧跟这个查询就是一个分页查询
List<Account> list = accountService.getAll();
//使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就可以了
PageInfo page = new PageInfo(list,5);
model.addAttribute("pageInfo", page);
return "account/list";
}
<% pageContext.setAttribute("path", request.getContextPath()); %>
<div class="col-md-6">当前${
pageInfo.pageNum }页,总${
pageInfo.pages }页,总${
pageInfo.total}条记录</div>
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${path}/AccountList?pn=1">首页</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li><a href="${path}/AccountList?pn=${pageInfo.pageNum - 1}"
aria-label="Previous"> <span aria-hidden="true">«</span>
</a></li>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#">${
page_Num }</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li><a href="${path}/AccountList?pn=${page_Num}">${
page_Num }</a></li>
</c:if>
</c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li><a href="${path}/AccountList?pn=${pageInfo.pageNum + 1}"
aria-label="Previous"> <span aria-hidden="true">»</span>
</a></li>
</c:if>
<li><a href="${path}/AccountList?pn=${pageInfo.pages}">末页</a></li>
</ul>
</nav>
</div>
10、SpringMVC中form标签
// 显示用户详细信息
@RequestMapping(value ="/getAccount/{id}",method = RequestMethod.GET)
public String getAccounts(@PathVariable("id") Integer id,Map<String,Object> map) {
map.put("account", accountService.selectAccountByPrimaryKey(id));
return "account/userInformation";
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="${pageContext.request.contextPath}/getAccount" method="get" modelAttribute="account">
账 户 :<form:input path="accountNumber" readonly="true"/><br>
</form:form>
11、工具
MD5加密
public class MD5Util {
public static String getMD5Str(String str) {
byte[] digest = null;
try {
MessageDigest md5 = MessageDigest.getInstance("md5");
digest = md5.digest(str.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//16是表示转换为16进制数
String md5Str = new BigInteger(1, digest).toString(16);
return md5Str;
}
}
生成指定位数的随机数
public class NumberGenerator {
//随机数工具方法
public static long getNumber(int length) {
StringBuilder buf = new StringBuilder();
Random random = new Random();
/*开头不为0,建议数据量较少时只放开部分,比如1至3开头的数,等业务达到一定数量时,再逐步放开剩余的号码段,由于是固定位数,总数量一定,生成的数越多,重复的几率越大**/
int firstNumber = random.nextInt(9) + 1;
buf.append(firstNumber);
for(int i = 0; i < length - 1; ++i) {
buf.append(random.nextInt(10));
}
return Long.valueOf(buf.toString());
}
}
生成系统时间
public class TimeSystem {
static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
public static String timeSystem = df.format(new Date());//new Date()为获取当前系统时间
}
Msg
public class Msg {
//状态码 100-成功 200-失败
private int code;
private String msg;
//用户返回给浏览器的数据
private Map<String,Object> extend = new HashMap<String,Object>();
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map<String, Object> getExtend() {
return extend;
}
public void setExtend(Map<String, Object> extend) {
this.extend = extend;
}
public static Msg success() {
Msg result = new Msg();
result.setCode(100);
result.setMsg("处理成功");
return result;
}
public static Msg fail() {
Msg result = new Msg();
result.setCode(200);
result.setMsg("处理失败");
return result;
}
public Msg add(String key,Object value) {
this.getExtend().put(key, value);
return this;
}
}
ps:本文借鉴了网上大佬们的博客
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/148614.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...