IDEA中,在Servlet里面运用@RequestMapping无法获取到前端jsp页面中的参数
前端login.jsp页面代码
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>登录页面</title>
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div id="header">
<div class="cover">
<span class="text0">欢迎访问大学生家教服务系统</span>
</div>
</div>
<div id="nav">
<div class="leftbox">
</div>
<div id="login">
<div class="form">
<a href="open.jsp">返回初始页面</a>
<form id="loginForm" action="LoginController" method="post">
用  户:<input id="username" type="text" name="username"><br>
密  码:<input id="password" type="password" name="password"><br>
身  份:<select id="identity" name="identity">
<option value="学生">学生</option>
<option value="教师">教师</option>
<option value="中介">中介</option>
<option value="管理员">管理员</option>
</select><br>
验 证 码:<input type="text" id="captcha" name="captcha"><br>
<img :src="captchaUrl" @click="clickImg" width="135px" height="33px" />
<button type="submit" onclick="validateForm()">登录</button>
</form>
</div>
</div>
<div class="rightbox">
</div>
</div>
<div id="banner">
</div>
<div id="footer">
</div>
<!--<audio controls autoplay>-->
<!-- <source src="http://music.163.com/song/media/outer/url?id=2095353936.mp3" autoplay >-->
<!-- Your browser does not support the audio element.-->
<!--</audio>-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script type="text/javascript">
new Vue({
el:'#login',
name:"login",
data(){
return{
admin:{},
captchaUrl:'/captcha'
}
},
methods: {
clickImg() {
this.captchaUrl = '/captcha?' + Math.random();; // 更新 URL 以防止缓存
}
},
mounted() {
this.clickImg(); // 组件挂载时初始化验证码图片
}
});
</script>
<script>
function validateForm() {
var password = document.getElementById('password').value;
if (!password.match(/^(?=.*\d)(?=.*[a-zA-Z])(.{4,})$/)) {
alert('密码因包含数字和字母且至少有4位');
return false;
}
}
</script>
</body>
</html>
后端LoginController.java代码
package Login;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@CrossOrigin
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
private LoginService loginService;
private String very;
@Override
public void init() throws ServletException {
System.out.println("0000");
loginService = new LoginService(new LoginDAO());
}
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("11111111111111111111111");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
SpecCaptcha captcha=new SpecCaptcha(135,33,4);
captcha.setCharType(com.wf.captcha.base.Captcha.TYPE_ONLY_NUMBER);
CaptchaUtil.out(captcha,request,response);
System.out.println("222222222222222222222222");
}
@PostMapping("/LoginController")
public String login(@RequestBody Login user, HttpServletRequest request){
System.out.println("33333333333333333333333333333333");
if(!CaptchaUtil.ver(user.getCaptcha(), request)){
CaptchaUtil.clear(request);
}
System.out.println(user.getCaptcha()+request);
System.out.println("4444444444444444444444444444444");
return "验证码错误";
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String captcha = request.getParameter("captcha");
String identity = request.getParameter("identity");
HttpSession session = request.getSession();
Login login = new Login(username, password, identity, captcha,very);
if(loginService.Login(login).getReason()==null){
LoginResult result=loginService.Login(login);
int uId = result.getId();
session.setAttribute("uId", uId);
session.setAttribute("identity",identity);
session.setAttribute("verycode",very);
System.out.println(uId);
System.out.println(identity);
System.out.println(very);
if(Objects.equals(identity, "教师")){
request.getRequestDispatcher("/teacher.jsp").forward(request, response);
}
else {
request.getRequestDispatcher("/loginSuc.jsp").forward(request, response);
}
}
else{
LoginResult result = loginService.Login(login);
request.setAttribute("reason", result);
request.getRequestDispatcher("/loginFailure.jsp").forward(request, response);
}
}
}
在这个代码里面,我前端的验证码:
<img :src="captchaUrl" @click="clickImg" width="135px" height="33px" />
<script type="text/javascript">
new Vue({
el:'#login',
name:"login",
data(){
return{
admin:{},
captchaUrl:'/captcha'
}
},
methods: {
clickImg() {
this.captchaUrl = '/captcha?' + Math.random();; // 更新 URL 以防止缓存
}
},
mounted() {
this.clickImg(); // 组件挂载时初始化验证码图片
}
});
</script>
后端的代码
```java
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("11111111111111111111111");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
SpecCaptcha captcha=new SpecCaptcha(135,33,4);
captcha.setCharType(com.wf.captcha.base.Captcha.TYPE_ONLY_NUMBER);
CaptchaUtil.out(captcha,request,response);
System.out.println("222222222222222222222222");
}
@PostMapping("/LoginController")
public String login(@RequestBody Login user, HttpServletRequest request){
System.out.println("33333333333333333333333333333333");
if(!CaptchaUtil.ver(user.getCaptcha(), request)){
CaptchaUtil.clear(request);
}
System.out.println(user.getCaptcha()+request);
System.out.println("4444444444444444444444444444444");
return "验证码错误";
}
后端的RequestMapping获取不到前端的参数,函数中的几个输出一个都没有实现.