现在的位置: 首页 > 综合 > 正文

springMVC学习记录

2013年07月22日 ⁄ 综合 ⁄ 共 4240字 ⁄ 字号 评论关闭

general.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="project_name" scope="session">zshの白老鼠</c:set>
<c:set var="project_url" scope="session">http://localhost:8080/Kata</c:set>
<c:set var="encoding" scope="session">UTF-8</c:set>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="general.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${project_name}</title>
</head>
<body>
	${user.userName} <br />
	${user.userPwd}
</body>
</html>

index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="general.jsp" %>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=${encoding}">
		<title>${project_name}</title>
		<style>
			.border{border:1px solid red;}
			.center{text-align:center;}
			#mainBody{margin:auto auto;width:1024px;height:768px;}
			#box1{margin:auto auto;width:400px;height:200px;}
		</style>
	</head>
	<body>
		<div id="mainBody" class="border">
			<div id="box1" class="border">
				<form action="${project_url}/d3/info" method="post">
					<table>
						<tr>
							<td>账号</td>
							<td><input type="text" name="userName" /></td>
						</tr>
						<tr>
							<td>密码</td>
							<td><input type="password" name="userPwd" /></td>
						</tr>
						<tr>
							<td colspan="2" class="center"><input type="submit" value="登录" /></td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</body>
</html>

template.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="general.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${project_name}</title>
</head>
<body>
	这是hello.jsp<br />
	${message} <br />
	version : ${version}
</body>
</html>

web.xml

<?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>Kata</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- filter -->
  <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>
  <!-- filter_end -->
  
  <!-- servlet -->
  <servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- servlet_end -->
</web-app>

test-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- HandlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

	<!-- HandlerAdapter -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

	<!-- ViewResolver -->
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	    <property name="prefix" value="/"/>
	    <property name="suffix" value=".jsp"/>
	</bean>

	<!-- 处理器 -->
	<bean class="controller.CommandTestController" />
	
</beans>

CommandTestController.java(控制器)

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import entity.User;
import global.Common;

@Controller
@RequestMapping(value="/d3")
public class CommandTestController{ //extends AbstractCommandController{
	
	@RequestMapping(value={"/info","/general"}, method = RequestMethod.POST)
	public ModelAndView test(HttpServletRequest req, HttpServletResponse resp, User user) {
		Common.print(this);
		ModelAndView mv = new ModelAndView();
		mv.addObject("user", user);
		boolean a = mv.hasView();
		mv.setViewName("hello");
		return mv;
	}
}

抱歉!评论已关闭.