发表于: 2017-06-12 00:01:49

1 1047


原本只打算看两个小时的,结果到现在。

今天所做:

从实现一个HelloWorld来学springmvc。实际上我是跟着别人做的。


1.新建了一个Dynamic Web Project。要新建Dynamic Web Project之前还要先help—install new Software,安装web插件。我本不打算说出我之前不知道为何我新建不了此类项目的事情的。

2.配置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>web</display-name>

  

  <servlet>

  <servlet-name>springmvc</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 

 <!-- springmvc默认加载springmvc的配置文件 需要满足命名规则 servlet-name-servlet.xml ==>springmvc-servlet.xml 和路径规则:springmvc-servlet.xml必须放到WEB-INF下

  <init-param>

  <param-name>contextConfiglocation</param-name>

  <param-value>classpath:springmvc.xml</param-value>

</init-param>-->

 

 </servlet>

  <servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

  

  <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>

</web-app>

3.配置springmvc.xml 。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.2.xsd 

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- 配置处理器映射器 springmvc默认的处理器映射器

根据bean(自定义collerter)的name属性的url寻找执行类controller-->

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!--配置处理器适配器:负责执行UserController-->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> 

<!--配置自定义bean :UserController-->

<bean name="/hello.do" class="com.xx.controller.UserController"></bean>

</beans> 

4.定义controller

代码会有红线,此时要导入tomcat包

导tomcat包:wiodow----→Preferencs---→server-----→Runtime-→Environments→进入一个对话框单击Add→添加安装的Tomcat版本→finish

右键项目——properties——buildpath——add library——server runtime

package com.xx.controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class UserController implements Controller{

@Override

public ModelAndView handleRequest(HttpServletRequest request, 

HttpServletResponse response) throws Exception {

// 接受请求 接受参数 验证参数 处理请求

//封装参数 调用业务方法,返回处理结果数据

ModelAndView mv = new ModelAndView();

//给ModelAndView设置数据 可以在页面回显

mv.addObject("hello","Hello World");

//设置物理视图

mv.setViewName("/WEB-INF/jsps/index.jsp");

return mv;

}

}

4.WEB-INF下建index.jsp,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<h1>${hello}</h1>

</body>

</html>

控制台出现:

通过 http://localhost:8080/SpringMVC/hello.do 

所遇问题:

我的web.xml修改了默认加载springmvc.xml位置,但是报错。我按照注释上的,把springmvc.xml复制一份放到WEB-INF下,命名为servlet-springmvc.xml就好了。那别人干嘛要改这个位置。按默认就好了嘛。不过不重要,重要的是我还得理解下这个例子的实现过程。




返回列表 返回列表
评论

    分享到