<?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>SpringMVC_HelloWorld</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>
<!-- spring mvc 的servlet -->
<!-- DispatcherServlet在初始化后会直接在/WEB-INF/下找springmvc-servlet.xml文件,
servlet-name标签的参数定义要和XML文件对应 -->
<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>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自动扫描的包 -->
<context:component-scan base-package="com.lin.helloworld.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- controller 返回的一个逻辑视图名经过前后缀的处理返回物理视图 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
package com.lin.helloworld.domain;
public class HelloWorld {
private String data;
public HelloWorld() {
super();
}
public HelloWorld(String data) {
super();
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "HelloWorld [data=" + data + "]";
}
}
package com.lin.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lin.helloworld.domain.HelloWorld;
@Controller
public class HelloWorldController {
//这里的/hello相当于struts2里的一个action
//返回一个字符串给视图
@RequestMapping("/hello")
public ModelAndView sayHello()
{
//modelAndView的构造方法的第一个参数相当于Struts2里的一个result的name
ModelAndView modelAndView = new ModelAndView("helloworld", "msg", "HelloWorld!!!");
return modelAndView;
}
//返回一个对象给视图
//@ModelAttribute("obj")的作用相当于Struts2的action类里面的一个field,
//用于表单提交的数据放进一个对象里面
//这里和struts2的区别:
//struts2处理表单提交的方式是:<input name="obj.data"/> 提交的数据封装在obj对象的data里面
//springmvc的方式是:<input name="data"/> 提交的数据封装在obj对象的data里面,
//前提是要使用@ModelAttribute注解
@RequestMapping("/helloObj")
public ModelAndView sayHelloWorld(@ModelAttribute("obj") HelloWorld obj)
{
System.out.println(obj.toString());
ModelAndView modelAndView = new ModelAndView("helloworld", "obj", obj);
return modelAndView;
}
}
<%@ 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%>" rel="external nofollow" >
<title>My JSP 'helloworld.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" rel="external nofollow" >
-->
</head>
<body>
HelloWorld! This is a spring mvc framework example.<br>
${msg}
<hr/>
<form action="helloObj" method="post">
<!-- 这里的表单提交和struts2不同的是name="data"会自动对应上对象的filed -->
<input type="text" name="data" size="30"/><br/>
<input type="submit" value="Submit"/>
</form>
<hr/>
${obj.data}
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有