Spring2.5注解 折磨了我N久的Helloworld

xiaoxiao2022-06-13  34

就是一个Helloworld ,看项目名 忍不住了 唉  只因   <context:component-scan base-package="com.ds.web"/> 中  com.ds.web  -------------------------cn.ds.web      做人要小心 做男人更要小心 Spring MVC 第一个Helloworld   首先 我们先把添加Spring2.5支持,配置Web.xml <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  <? xml version = " 1.0 "  encoding = " UTF-8 " ?>  2  < web - app version = " 2.5 "  xmlns = " http://java.sun.com/xml/ns/javaee "  3      xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "  4      xsi:schemaLocation = " http://java.sun.com/xml/ns/javaee   5      http: // java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  6       < context - param >  7           < param - name > contextConfigLocation </ param - name >  8           < param - value >  9               / WEB - INF / hello - data.xml  / WEB - INF / applicationContext.xml 10           </ param - value > 11       </ context - param > 12  13       <!--   Spring 容器启动监听器  --> 14       < listener > 15           < listener - class > 16              org.springframework.web.context.ContextLoaderListener 17           </ listener - class > 18       </ listener > 19  20       <!--  编码过滤器  --> 21       < filter > 22           < filter - name > CharacterEncodingFilter </ filter - name > 23           < filter - class > 24              org.springframework.web.filter.CharacterEncodingFilter 25           </ filter - class > 26           < init - param > 27               < param - name > encoding </ param - name > 28               < param - value > UTF - 8 </ param - value > 29           </ init - param > 30           < init - param > 31               < param - name > forceEncoding </ param - name > 32               < param - value > true </ param - value > 33           </ init - param > 34       </ filter > 35       < filter - mapping > 36           < filter - name > CharacterEncodingFilter </ filter - name > 37           < url - pattern > /* </url-pattern> 38      </filter-mapping> 39  40  41  42      <!-- Spring MVC --> 43      <servlet> 44          <servlet-name>hello</servlet-name> 45          <servlet-class> 46              org.springframework.web.servlet.DispatcherServlet 47          </servlet-class> 48          <load-on-startup>0</load-on-startup> 49      </servlet> 50      <servlet-mapping> 51          <servlet-name>hello</servlet-name> 52          <url-pattern>*.htm</url-pattern> 53      </servlet-mapping> 54  55      <session-config> 56          <session-timeout>60</session-timeout> 57      </session-config> 58  59      <welcome-file-list> 60          <welcome-file>index.jsp</welcome-file> 61      </welcome-file-list> 62  </web-app> 63  再来 写个controller <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  package  com.ds.web;  2   3  import  java.io.IOException;  4  import  java.io.PrintWriter;  5   6  import  javax.servlet.http.HttpServletRequest;  7  import  javax.servlet.http.HttpServletResponse;  8   9  import  org.springframework.stereotype.Controller; 10  import  org.springframework.web.bind.annotation.RequestMapping; 11  12  13  14  15  @Controller 16  @RequestMapping( " /hello.htm " ) 17  public   class  HelloController { 18       19      @RequestMapping(params  =   " method=forwardHello " ) 20       public   void  forwardHello(HttpServletRequest request,HttpServletResponse response)  throws  IOException { 21           22          System.out.println(request.getParameter( " name " ) + " 成功的使用了HelloWorld " ); 23           24          PrintWriter out  =  response .getWriter(); 25          response.setContentType( " text/html " ); 26          response.setCharacterEncoding( " utf-8 " ); 27          out.write(request.getParameter( " name " ) + " 成功的使用了HelloWorld " ); 28      } 29       30  } 31  第3 配置hello-servlet.xml <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  < beans xmlns = " http://www.springframework.org/schema/beans "  2      xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "  3      xmlns:p = " http://www.springframework.org/schema/p "  4      xmlns:aop = " http://www.springframework.org/schema/aop "  5      xmlns:context = " http://www.springframework.org/schema/context "  6      xmlns:jee = " http://www.springframework.org/schema/jee "  7      xmlns:tx = " http://www.springframework.org/schema/tx "  8      xsi:schemaLocation = "  9             http: // www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 10             http: // www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 11             http: // www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd 12             http: // www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 13             http: // www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> 14  15       <!-- 视图解析链   --> 16       < bean  class = " org.springframework.web.servlet.view.InternalResourceViewResolver "  p:prefix = " / " 17              p:suffix = " .jsp "  p:viewClass = " org.springframework.web.servlet.view.JstlView " /> 18               19       <!--  ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射  --> 20       < bean  class = " org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter " />          21               22       <!--  对Controll组件进行扫描   通常要使用@Autowried(要进行自动bean的自动装配)都要进行一些xml配置可以通过这个标签来简化配置 --> 23       < context:component - scan base - package = " com.ds.web " /> 24  </ beans > 第4 写个jsp <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  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() + path + " / " ;  5  %>  6   7  <! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >  8  < html >  9     < head > 10       < base href = " <%=basePath%> " > 11       12       < title > My JSP  ' index.jsp '  starting page </ title > 13       < meta http - equiv = " pragma "  content = " no-cache " > 14       < meta http - equiv = " cache-control "  content = " no-cache " > 15       < meta http - equiv = " expires "  content = " 0 " >      16       < meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " > 17       < meta http - equiv = " description "  content = " This is my page " > 18       <!-- 19       < link rel = " stylesheet "  type = " text/css "  href = " styles.css " > 20       --> 21       22     </ head > 23     24     < body > 25       < form action = " hello.htm?method=forwardHello "  method = " post " > 26                   < span > Text: </ span >< input type = " text "  name = " name " > 27                   < input type = " submit "  value = " Submit " > 28               </ form > 29     </ body > 30  </ html > 31  OK Run Sucess 相关资源:Java 面经手册·小傅哥(公众号:bugstack虫洞栈).pdf
转载请注明原文地址: https://www.6miu.com/read-4936262.html

最新回复(0)