springmvc
来源:互联网 发布:数据修复 编辑:程序博客网 时间:2024/05/16 15:24
第一步:新建一个动态web项目
第二部导入相应的spring架包,可以将lib中的架包全导入到项目中,另外再导入logging架包,并且将上面的所有的架包手动复制一份到webContent下的web-inf中的lib中
第三步:如上图在web-inf下的lib中新建一个web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>spring2</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>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 查找springtest3-servlet.xml -->
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在新建一个**-servlet.xml文件,这里我建了一个springMVC-servlet.xml(注意:这里你自己可以随便命名但是在建好后运行tomcat时会相应的路径里的这个文件找不到这时可以复制报错路径中的文件名将此处文件名改下就行)
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--(视图分解)对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<bean name="/test1.helloworld" class="com.web.controller.HelloWorldController">
</bean>
</beans>
写上面的xml文件时一般我们会从网上复制代码,但是会报错一般报无法解析(如
cvc-complex-type.2.3: Element 'beans' cannot have character [children]
),常常原因是你复制下来的代码含有非法字符,找到并去掉就行,第四步:建一个controller类结构如下:
类内容如下:
package com.web.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 HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("Hello World");
return new ModelAndView("/welcome");
}
}
这个类中拓展下如果你想查看ModelAndView这个类的源码,按住ctrl建选change Attached Source 选external folder不过我做的时候没成功,选的是external file选择你spring架包相对应的架包的source文件就是含有source字样的文件
第五步:在webContent下新建一个welcome.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
第一个springmvc程序
</body>
</html>
到此简单的程序已经完成,可以运行tomcat,可能会出现类找不到可以看路径文件名有没有错什么的;还有就是乱码,你要把workspace编码,jsp编码已经文件的编码,浏览器的编码都改成一致,这里用的UTF-8;
最后因为我是公司加域上网的运行时总是出不来,报404,这个问题你可以把网络断掉,在运行就ok了
- SpringMVC
- springmvc
- SpringMVC
- SpringMVC
- springMvc
- springMVC
- springmvc
- springMVC
- springMVC
- springmvc
- SpringMVC
- SpringMVC
- springMvc
- springmvc
- springmvc
- SpringMVC
- Springmvc
- springmvc
- html5 调用手机相机、文件和相册
- git windows下搭建过程
- android_多activity
- 关于升级了Xcode7以后遇到的问题(+今天看到的)
- XCode编译器里有鬼 – XCodeGhost样本分析
- springmvc
- Linux 内核 系列文章
- Git相关网站
- [Leetcode] 11 Container With Most Water
- Java代码测试大端小端
- Sublime汉化操作全解
- 牛逼程序员养成记
- Android Gradle加速编译
- Spring的OpenEntityManagerInViewFilter