spring mvc hello
【摘要】
1.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/200...
1.配置web.xml文件
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5"
-
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_2_5.xsd">
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
<servlet>
-
<servlet-name>hello</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
<load-on-startup>1</load-on-startup>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:spring-servlet-config.xml</param-value>
-
</init-param>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>hello</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
</web-app>
2.配置spring-servlet-config.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"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
-
<!-- 在spring中配置HandlerMapping、HandlerAdapter -->
-
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
-
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
-
<!-- ViewResolver -->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
-
<property name="prefix" value="/"></property>
-
<property name="suffix" value=".jsp"></property>
-
</bean>
-
<bean name="/hello" class="com.controller.HelloController"></bean>
-
</beans>
3.控制器代码;
-
package com.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 HelloController implements Controller {
-
-
public ModelAndView handleRequest(HttpServletRequest arg0,
-
HttpServletResponse arg1) throws Exception {
-
//1.收集参数,验证参数;
-
//2.绑定参数到命令对象;
-
//3.将命令对象传入业务对象,进行业务处理;
-
//4.选择下一个页面;
-
ModelAndView mv=new ModelAndView();
-
//添加模型和数据可以是任意的POJO对象;
-
mv.addObject("message","hello world"); //这是数据,相当于model
-
//设置逻辑视图名,视图解析器会根据名字,将之解析到具体的视图页面;//设置view
-
mv.setViewName("hello");
-
return mv;
-
}
-
-
}
4.前端页面
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'index.jsp' starting page</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
-
<body>
-
<!-- 这是创建的hello.jsp页面 ;message:相当于我们的设置属性-->
-
${message }
-
</body>
-
</html>
使用注解的话,
SpringMVC 的注解和 XML 冲突的问题
There is already handler of type [class com.sdemo.controller.Login] mapped.原因在于spring配置文件中和控制器注解都定义过了。
<bean name="/hello" class="com.controller.HelloController"></bean>
@Controller
public class HelloController {
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/69062751
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)