controller調用controller的方法,SpringMVC_Controller注解與RequestMapping

 2023-10-09 阅读 27 评论 0

摘要:? 把這里的只勾選這一行,Eclipse啟動就快了。也不會有不該報錯的報錯。 ? index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>

?

把這里的只勾選這一行,Eclipse啟動就快了。也不會有不該報錯的報錯。

?

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() +request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head><base href="<%=basePath %>"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>new jsp</title>
</head>
<body><form action="<%=basePath %>logon2.htm" method=post><label>用戶名:</label><input type="text" name="username"><br><label>&nbsp;碼:</label><input type="password" name="password"><br><label>&nbsp;齡:</label><input type="text" name="age"><br><input type="submit" value="提交"></form>
</body>
</html>

FrontController.java

package cn.java.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller // 在一個普通類上一行加上@Controller就變成了Servlet了,就無需extends繼承Servlet了。
public class FrontController {@RequestMapping(value="/login.html") //為當前方法配置一個對外訪問的虛擬路徑,@RequestMapping是springmvc框架特有的,并可以處理get和post請求public void Login() {System.out.println("登錄成功");}//Servlet只能寫一個方法,而用了springmvc可以寫多個方法@RequestMapping(value= {"/register.htm","abc.html","hello.html"})  //可以配置很多個虛擬路徑public void Register() {System.out.println("注冊成功");}@RequestMapping(value= "/logon.htm") public void Logon(String username,String password) { //在進入地址http://localhost:8080/springMVC/logon.html?username=wang&password=123后得到的結果直接把username的值和password的值傳到方法里去了,而Servlet是要用request來取值的,springmvc簡單了很多System.out.println("用戶名為" + username);System.out.println("密碼為" + password);}@RequestMapping(value= "/logon2.htm",method=RequestMethod.POST) public String Logon2(User u) { //在進入地址http://localhost:8080/springMVC/logon2.html?username=wang&password=123后得到的結果直接把username的值和password的值傳到類名為User中去了,User類中寫好了username和password兩個屬性,還有setter getter方法,可以直接傳進User對象中去,并且可以轉好類型
        System.out.println(u);return "/success.jsp";}
}    

方法執行完跳轉到其他頁面:return "/success.jsp";

如果想用post傳輸方法,就在@RequestMapping里用method=RequestMethod.POST

這樣就只支持post而不支持get了

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" 
version="3.0"><display-name>springMVC</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 配置springMVC的核心控制器類:DispatcherServlet --><servlet><servlet-name>dispatcherServlet</servlet-name><!-- 按住ctrl + shift + H 輸入DispatcherServlet,ok后在文件右鍵復制路徑,粘貼到下面 --><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 將DispatcherServlet和springmvc.xml文件關聯起來 --><init-param><param-name>contextConfigLocation</param-name><!-- param-value:給springmvc起一個文件名,固定寫法,不能改 --><param-value>classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><!-- /*代表全部都攔截,/代表當前項目,*代表所有的,  如果是*.htm,那就攔截所有htm文件 --><url-pattern>*.htm</url-pattern></servlet-mapping>
</web-app>

?

轉載于:https://www.cnblogs.com/lonske/p/9096683.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/134399.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息