Springmvc

作者: xhlc02 | 来源:发表于2018-09-12 17:00 被阅读0次

目录:
1.新建一个maven项目,项目名Springmvc
2.项目建好后,pow.xml报错,因此需要建立一个WEB-INF和web.xml文件
3.在pow.xml里面加入依赖
4.在resource里面新建一个application-context.xml
5.编写web.xml文件
6.在包com.xdl.controller下面新建一个HelloController类


image.png

1.新建一个maven

image.png
image.png
2.项目建好后,pow.xml报错,因此需要建立一个WEB-INF和web.xml文件
image.png
3.在pow.xml里面加入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
4.在resource里面新建一个application-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com" />
<context:annotation-config />
</beans>
5.编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application-context.xml
</param-value>
</context-param>

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
4.在WEB-IN里面新建一个spring-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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.*" />
<mvc:annotation-driven />

<mvc:view-controller path="/" view-name="index"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>

6.在包com.xdl.controller下面新建一个HelloController类
package com.xdl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("list")
public class HelloController {
@RequestMapping("hello")
public void hello(HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
response.setCharacterEncoding("utf-8");//设置返回编码
response.setContentType("application/json");//设置返回类型
try {
PrintWriter printWriter=response.getWriter();
printWriter.println("hello:"+name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
此时可以运行Tomact进去测试
路径:http://localhost:8080/SpringmvcTest/list/hello
或者http://localhost:8080/SpringmvcTest/list/hello?name=haha

友情链接:https://shop117143218.taobao.com/shop/view_shop.htm?spm=a313o.201708ban.category.d53.64f0197aXY7Er3&mytmenu=mdianpu&user_number_id=1070606134

相关文章

网友评论

      本文标题:Springmvc

      本文链接:https://www.haomeiwen.com/subject/qotbgftx.html