当前位置: 代码迷 >> Java Web开发 >> springMVC 浏览器直接展示jsp源码。求解
  详细解决方案

springMVC 浏览器直接展示jsp源码。求解

热度:2242   发布时间:2016-04-11 00:05:19.0
springMVC 浏览器直接显示jsp源码。求解?
本帖最后由 shanxiuwei 于 2013-10-09 17:08:58 编辑
直接访问jsp也会显示源码.


controller代码

package com.controller;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.User;
import com.service.IUserService;
import com.validator.UserValidator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private UserValidator userValidator;

@RequestMapping(value = "/getUser/id/{userID}")
public ModelAndView getUser(@PathVariable int userID) throws Exception {
ModelAndView model=new ModelAndView("hello");
model.addObject("hello_1", userService.getUserByID(userID));
Logger logger = Logger.getLogger(UserController.class);
logger.info("test");
return model;
}

@RequestMapping(value="/listPageUser")
public ModelAndView listPageUser(User user){
ModelAndView model=new ModelAndView("hello_s");
model.addObject("hello_s", userService.listPageUser(user));
return model;
}

@RequestMapping(value = "/getStr")
@ResponseBody
public String getStr(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return "hello!";
}

@RequestMapping(value = "/getJSP")
public String getJSP(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello_1", userService.getUserByID(1));
return "hello";
}
@RequestMapping(value="/delete/id/{id}")
@ResponseBody
public String deleteUser(@PathVariable int id){
userService.deleteUserByID(id);
return "Success!";
}
@RequestMapping(value="/saveUser")
@ResponseBody
public String saveUser(User user,BindingResult result){
userValidator.validate(user,result);
if(result.hasErrors()){
return "fail!";
}
int userID=userService.saveUser(user);
return "userID="+userID;
}
@RequestMapping(value="/throwException")
public String throwException(){
int bbb=Integer.valueOf(null);
return "exception";
}
}


spring配置文件

<?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:aop="http://www.springframework.org/schema/aop" 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-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 连接池配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/myBatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- MyBatis的 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
  相关解决方案