SpringMVC 2.5 的HelloWorld

SpringMVC 2.5 的HelloWorld

首先,写一个Bean

package springmvc.one.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hellOne.act")
public class HelloOneAction { 

@RequestMapping
public String handleRequest(String user,Model model) { 
System.out.println("用户名:"+user); //GET/POST的入参
model.addAttribute("user", user); //通过Session返回到界面的出参
model.addAttribute("helloWord", "Hello");

return "hellouser";

}


再写一个JSP页面hellouser.jsp,此页面放在 WEB-INF/jsp 目录下,代码如下:

<html> 
<head><title>HelloPage</title></head> 
<body> 
Test this sample!
<H1> ${helloWord}, ${user}!</H2> 
</body>
</html>


接着看看web.xml的配置

<?xml version="1.0" encoding="ISO-8859-1"?> 

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<description>Spring 2.5 App</description> 
<display-name>Spring App Examples</display-name> 

<servlet> 
<servlet-name>annomvc</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup> 
</servlet> 

<servlet-mapping> 
<servlet-name>annomvc</servlet-name> 
<url-pattern>*.act</url-pattern> 
</servlet-mapping> 

</web-app>


最后就是,annomvc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="springmvc.one.web"/>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>


好了,见一个Tomcat工程,在 tomcat 中运行,访问下面连接,就可以运行了。

http://localhost:8080/TOMCAT-PROJECT/hellOne.act?user=gjhuai
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值