java学习第一篇 第一个springmvc项目

由于公司的项目转型,以后基本都要使用java了,10年的c#生涯要告一段落了。

今天是学习java的第2天,根据所学知识,自己做了第一个springmvc  demo。

一。创建springmvc 工程

file=》new=》other=》web=》dynamic web project 

二。添加相应的jar包

spring mvc 下载jar包地址https://repo.spring.io/libs-release-local/org/springframework/spring/4.3.4.RELEASE/spring-framework-4.3.4.RELEASE-dist.zip

commons-logging 下载地址 https://mirrors.tuna.tsinghua.edu.cn/apache//commons/logging/binaries/commons-logging-1.2-bin.zip

jstl 下载地址http://repo2.maven.org/maven2/javax/servlet/jstl/1.2/jstl-1.2.jar

三。添加controller

package springmvcfirest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class hellozw {

@RequestMapping("/hellozw1")
public String hellozw1(Model model)
{
model.addAttribute("msg","hello sprinhellozwgmvcfirest!");
return "hellozw";

}

四,添加jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> 
<div>
${msg}</div>
</body>
</html>

五。添加  springmvcfirest-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-4.2.xsd">


<!-- 自动扫描组件 -->
<context:component-scan base-package="springmvcfirest" />

<!-- 打开注解驱动 -->
<mvc:annotation-driven />

<!-- 配置jsp视图解析器 ,需要有jstl这个jar包,否则不能映射 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

六。修改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_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>springmvcfirest</display-name>
<servlet>
<servlet-name>springmvcfirest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvcfirest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list> 
<welcome-file>/WEB-INF/pages/Hello2.jsp</welcome-file>
</welcome-file-list>
</web-app>

7运行效果

ok,第一个springmvc项目就弄好啦

猜你喜欢

转载自www.cnblogs.com/mrzhongwei/p/10629217.html