改进配置信息
改进1;在web.xml中修改欢迎页的配置信息,并将index.jsp移动到jsp文件夹下
<!-- 项目欢迎页,首页 -->
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
改进2:在spmvc-servlet.xml文件中插入下面的代码
1 <!--在spring中一切资源都被视为bean --> 2 <bean id="simpleUrlHandlerMapping" 3 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 4 <property name="mappings"> 5 <props> 6 <prop key="/my.do">MyControl</prop> 7 <prop key="/login.do">LoginControl</prop> 8 </props> 9 </property> 10 </bean> 11 <!-- 在访问的jsp页面的前后加上前缀和后缀 --> 12 <bean id="viewResolver" 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/jsp/" /> 15 <property name="suffix" value=".jsp" /> 16 </bean> 17 18 <!-- 说明: my.do 的请求将给名为 MyControl 的 bean 进行处理。 --> 19 <bean id="MyControl" class="com.test.MyController"></bean> 20 <bean id="LoginControl" class="com.test.LoginControl"></bean>
改进3:改动MyController.java文件
编写:LoginControl.java文件
1 package com.test; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class LoginControl implements Controller{ 10 11 @Override 12 public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { 13 // TODO Auto-generated method stub 14 ModelAndView mv = new ModelAndView("login"); 15 mv.addObject("什么的干活", "dfdsfs"); 16 return mv; 17 } 18 19 }
改进4.编写login.jsp文件
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 5 + path + "/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP ‘login.jsp‘ starting page</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 <form action="post" method="get"> 28 First name:<br> <input type="text" name="firstname"> <br> 29 Last name:<br> <input type="text" name="lastname"> 30 31 </form> 32 ${什么的干活} 33 34 </body> 35 </html>
5.访问网页的问题
1.启动项目
2.打开浏览器
打开了项目的首页;
<a href="http://localhost:8080/my_web_spring/jsp/login.jsp">登录界面</a>
3.在浏览器地址栏后面输入my.do
返回在java文件中的信息
4.点击链接,跳转到登录界面
5.
原文地址:https://www.cnblogs.com/xyblogs/p/9026474.html
时间: 2024-10-04 16:47:52